Search code examples
rtwitteroauthtwitter-oauth

Unauthorized error with Twitter OAuth


I noticed many threads regarding problems with Twitter API authentication but apparently no one seem relevant to my problem. As soon as I try to authenticate, right after I am asked to enter the PIN, the system throws an error ("Unauthorized"). This happens before I can enter the PIN.

The code is:

library("twitteR")
library("RCurl")
library("ROAuth")

# Set SSL certs globally
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

Credentials <- OAuthFactory$new(
  consumerKey = "XX",
  consumerSecret = "XX",
  oauthKey = "XX",
  oauthSecret = "XX",
  requestURL = "https://api.twitter.com/oauth/request_token",
  authURL = "https://api.twitter.com/oauth/authorize",
  accessURL = "https://api.twitter.com/oauth/access_token")

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

And the results is:

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

As mentioned above, this happens before I can actually enter my PIN. I run the script in RStudio but running on classic R GUI does not make any change. I am running R version 3.0.1.

Post Scriptum I tried different version of the code, for example this one, but I get the exact same error.


Solution

  • You can follow this step:

    reqURL <- "https://api.twitter.com/oauth/request_token"
    accessURL <- "https://api.twitter.com/oauth/access_token"
    authURL <- "https://api.twitter.com/oauth/authorize"
    consumerKey <- "Mjn6tdsadsadkasdklad2SV1l"
    consumerSecret <- "58Z7Eldsdfaslkf;asldsaoeorjkfksaVCQtvri"
    twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                                 consumerSecret=consumerSecret,
                                 requestURL=reqURL,
                                 accessURL=accessURL,
                                 authURL=authURL)
    twitCred$handshake()
    

    After you run this code you will see in R console message like this :

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

    Just paste the link to your browser then authorize app, last one you will get the PIN code, just copy and paste the PIN code to your R console.

    registerTwitterOAuth(twitCred)
    

    R console will show TRUE if you success.

    user <- getUser("xxx")
    userTimeline(user, n=20, maxID=NULL, sinceID=NULL, includeRts=FALSE)
    

    If still any problem just try to show your package version and update to the last version

    sessionInfo()
    update.packages("twitteR")
    

    The last version for twitteR is 1.1.7 => http://cran.r-project.org/web/packages/twitteR/index.html

    You can download twitteR manual => see page number 12 http://cran.r-project.org/web/packages/twitteR/twitteR.pdf