r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

demo: oauth1-twitter.r does not work #195

Closed petermeissner closed 9 years ago

petermeissner commented 9 years ago

The demo shown in oauth1-twitter.r does not work (tested on Win7 / Win8 with up to date httr from CRAN)

require(httr)
## Loading required package: httr
#1. Find OAuth settings for twitter:
#    https://dev.twitter.com/docs/auth/oauth
oauth_endpoints("twitter")
## <oauth_endpoint>
##  request:   https://api.twitter.com/oauth/request_token
##  authorize: https://api.twitter.com/oauth/authenticate
##  access:    https://api.twitter.com/oauth/access_token
#2. Register an application at https://apps.twitter.com/
#    Insert your values below - if secret is omitted, it will look it up in
#    the TWITTER_CONSUMER_SECRET environmental variable.
myapp <- oauth_app("twitter", 
                   key = "KeyForTwitterApp", 
                   secret="TheAppsTwitterSecret")

#3. Get OAuth credentials
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
## Error in init_oauth1.0(self$endpoint, self$app, permission = self$params$permission): client error: (401) Unauthorized
#4. Use API
req <- GET("https://api.twitter.com/1.1/statuses/home_timeline.json",
  config(token = twitter_token))
## Error in config(token = twitter_token): object 'twitter_token' not found
stop_for_status(req)
## Error in status_code(x): object 'req' not found
content(req)
## Error in inherits(x, "response"): object 'req' not found
sessionInfo()
## R version 3.1.2 (2014-10-31)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 
## locale:
## [1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
## [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
## [5] LC_TIME=German_Germany.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] httr_0.6.1
## 
## loaded via a namespace (and not attached):
##  [1] bitops_1.0-6    digest_0.6.8    evaluate_0.5.5  formatR_1.0    
##  [5] htmltools_0.2.6 knitr_1.8       R6_2.0.1        RCurl_1.95-4.5 
##  [9] rmarkdown_0.4.2 stringr_0.6.2   tools_3.1.2     yaml_2.1.13
hadley commented 9 years ago

Works for me. Auth failure usually indicates you've set up your app incorrectly. Make sure you've set the callback url to "http://127.0.0.1:1410"

petermeissner commented 9 years ago

That was the missing part. Thanks.

prof-joe commented 9 years ago

If I set the callback URL to localhost:1410 twitCred$handshake(cainfo="cacert.pem") does not work, and if set this to space, the browser appears correctly. On the other hand, it seems that setup_twitter_oauth(consumerKey, consumerSecret) requires localhost:1410 but the R terminal disappears then. Can you explain why? I think many people are in trouble.

petermeissner commented 9 years ago

I remember reading a lot about TwitterAPI not working with localhost as callback URL. Try replacing ...

localhost:1410

... by verbose ...

http://127.0.0.1:1410

... and see if this solves the problem.

prof-joe commented 9 years ago

I did it. The situation remains the same.

schnee commented 9 years ago

the "http://127.0.0.1:1410" solution resolved the issue I was having with this.

briholt100 commented 9 years ago

I'm having the same problem, even after making the callback = http://127.0.0.1:1410/

After executing this code: twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)

Rstudio gives me this message while alerting me to a fatal error, wishing to restart a session:

Waiting for authentication in browser.... press ESC/ctrl + C to abort Error in withCallingHandlers(trycatch(evalq((function (has = TRUE, parent = parent.frame(),: object '.rcpp_warning_recorder' not found

I've been trying this on 4 different platforms: Windows 7 and 8, and a linux mint and lubuntu. I've also tried on 2 different locations, one at home (win 8 and the 2 linux machines) and one at work (the win 7).

Where should I begin to trouble shoot this?

hadley commented 9 years ago

@briholt100 that's a different issue, and can be solved by reinstalling Rcpp.

briholt100 commented 9 years ago

Thank you. That worked for my Window machines. But it didn't help with the linux machines. I'll have to dig a bit more. Thanks again.

briholt100 commented 9 years ago

my linux machines just needed RCurl reinstalled and httr is working. Still learning; thanks for your help.

ncdingari commented 9 years ago

@prof-joe does it finally work? I am having same issue. Handshake does not work and says "Can not connect to server 127.0.0.1"

isomorphisms commented 8 years ago

This helped me with the same error http://stackoverflow.com/a/29505711/563329

mikefantast commented 8 years ago

Thank you for this! It was driving me crazy.

aishwarya91 commented 7 years ago

Hi @geoffjentry i have tried almost everything suggested by most of the people here but still i am getting the same error. can someone please help me out with this.

setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret) [1] "Using direct authentication" Error in check_twitter_oauth() : OAuth authentication error: This most likely means that you have incorrectly called setup_twitter_oauth()'

geoffjentry commented 7 years ago

@aishwarya91 Hi - your comment is about twitteR, not httralthough it's possible that the underlying cause leads you back here anyways as the former acts mainly as a passthrough for the latter.

Also note that twitteR is deprecated in favor of rtweet from @mkearney so you might want to look into using that instead.