简体   繁体   中英

Invalid Request token using OAuth and R - Is the R-code correct?

I'm revising my original query to ask specifically if anyone can see a reason that the R-code I give below would, in of itself, lead to an invalid request token. If not, I'll know my issue lies elsewhere.

I'm trying to use the twitteR package as I've gotten to the Twitter chapter in Jeffrey Stanton's free e-book on Data science which is helping me learn R. I get so far and then Twitter gives me this message:

Whoa there! The request token for this page is invalid. It may have already been used, or expired because it is too old. Please go back to the site or application that sent you here and try again; it was probably just a mistake.

I've got the bitops, RCurl, RJSONIO, twitter and ROAuth packages installed. I then run this code(NB I can't post links because my reputation is too low):

reqURL <- "//api.twitter.com/oauth/request_token"
   accessURL <- "//api.twitter.com/oauth/access_token"
   authURL <- "//api.twitter.com/oauth/authorize"
   consumerKey <- "abc"
   consumerSecret <- "xyz"
   twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                                consumerSecret=consumerSecret,
                                requestURL=reqURL,
                                accessURL=accessURL,
                                authURL=authURL)

Followed by this:

twitCred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )

I then get this prompt:

To enable the connection, please direct your web browser to: http://api.twitter.com/oauth/authorize?oauth_token=Kxa ... When complete, record the PIN given to you and provide it here:

At this point I can't seem to copy the twitter link, so I hand type it(!) into my browser and I get the message from Twitter I started my question with. I've tried a number of times with the same outcome. The OAuth bit in the TwitteR vignette on r-project doesn't have anything on this (it assumes I'd have done it right I suppose!) and the example on page nine of the TwitteR package description doesn't help me either.

Thanks.

I've kind of answered my original query myself. I quit the R-studio project I was working in, recreated my access token and access token secret from within the twitter developer site, and then applied this code in a new R-studio project:

requestURL <-  "https://api.twitter.com/oauth/request_token"
accessURL =    "https://api.twitter.com/oauth/access_token"
authURL =      "https://api.twitter.com/oauth/authorize"
consumerKey =   "------------"
consumerSecret = "-----------"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                         consumerSecret=consumerSecret,
                         requestURL=requestURL,
                         accessURL=accessURL,
                         authURL=authURL)
download.file(url="http://curl.haxx.se/ca/cacert.pem",
          destfile="cacert.pem")
twitCred$handshake(cainfo="cacert.pem")

I got that from here: TwitteR, ROAuth and Windows: register OK, but certificate verify failed

This combination of things stopped me getting the message from twitter that the request token was invalid. So I then get the link to twitter which now works: To enable the connection, please direct your web browser to: https://api.twitter.com/oauth/authorize?oauth_token=xxxx

However I didn't get much further, so I've written a new question here: Where does twitteR PIN code appear during R oauth authentication?

I faced the same issue . I managed in two ways: (1) Use R console instead of R studio OR (2) You take snapshot of the link and convert into text using OCR software(this)

Thanks and regards!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM