Mehar Ali
Mehar Ali

Reputation: 79

Authorization Required error when accessing Twitter API

I've found the code below in a tutorial, but after executing this handshake command it gives me the following error:

Error: Authorization Required

Can you help me to resolve this issue?

library(twitteR)
library(ROAuth)

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

Upvotes: 2

Views: 4906

Answers (2)

MarkeD
MarkeD

Reputation: 2631

It may be because you are requesting from http and not https for the authURL and accessURL.

Here is some code I use and works:

require(twitteR)

api_key = "XXXXX"
api_secret = "XXXXX"

TwitterOAuth<-function(){
  reqURL <- "https://api.twitter.com/oauth/request_token"
  accessURL <- "https://api.twitter.com/oauth/access_token"
  authURL <- "https://api.twitter.com/oauth/authorize"
  twitCred <- OAuthFactory$new(consumerKey=api_key, 
                               consumerSecret=api_secret, 
                               requestURL=reqURL,
                               accessURL=accessURL,
                               authURL=authURL)
  options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
  twitCred$handshake()
  registerTwitterOAuth(twitCred)
  }

TwitterOAuth()

I have some more details in a blogpost I did a while ago.

Upvotes: 0

Frostbite
Frostbite

Reputation: 106

If you are using the CRAN version of twitteR(1.1.7) try using the GitHub version (1.1.8).

The 1.1.8 version useses httr instead of ROAuth which might help link

Download from GitHub:

library(devtools)
library(httr)
install_github("twitteR", username = "geoffjentry")
library(twitteR)

You might get a Warning message: Username parameter is deprecated. Please use geoffjentry/twitteR

But you should ind twitteR in your library anyway.

And then with twitteR 1.1.8

library(httr)
library(twitteR)
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
tweets <- searchTwitter('#bigdata', n=150) 
tweets.df <- do.call(rbind, lapply(tweets, as.data.frame))
write.csv(tweets.df, "C:/...")

Hope this helps!

Upvotes: 4

Related Questions