I am trying to analyze some tweets using R and twitteR package. The handshake and registration code seems to work properly and I get the authorization link from R. However, when I enter the PIN obtained from https://api.twitter.com/oauth/authorize I get a "Forbidden Error." Any help is appreciated.
The Code:
TwitterOAuth<-function(){
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- "xxxxxxxxxxxxxxxx"
consumerSecret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL)
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
twitCred$handshake()
registerTwitterOAuth(twitCred)
}
The Response:
TwitterOAuth()
To enable the connection, please direct your web browser to:
http://api.twitter.com/oauth/authorize?oauth_token=X0AwET4FXBC7YRIWWN3iF61WFNE1DjxbfibqtfFjgcc
When complete, record the PIN given to you and provide it here: 1998913
Error: Forbidden
My sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Turkish_Turkey.1254 LC_CTYPE=Turkish_Turkey.1254 LC_MONETARY=Turkish_Turkey.1254
[4] LC_NUMERIC=C LC_TIME=Turkish_Turkey.1254
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] twitteR_1.1.7 rjson_0.2.13 ROAuth_0.9.3 digest_0.6.4 RCurl_1.95-4.1 bitops_1.0-6
loaded via a namespace (and not attached):
[1] tools_3.0.2
Change your access URL from http to https.
You can follow this step (don't forget, valid URL is use https):
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)
Based on this blog: http://thinktostart.wordpress.com/2013/05/22/twitter-authentification-with-r/
library(RCurl)
# Set SSL certs globally
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
require(twitteR)
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
consumerKey <- "yourconsumerkey"
consumerSecret <- "yourconsumersecret"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
twitCred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
registerTwitterOAuth(twitCred)
I had the same "Error: forbidden" response and haven't resolved it today (although I did previously).
My R script for my Windows 8.1 system also includes this line:
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
I found this line online with the comment to it, "this is a necessary step for Windows"
About three days ago I successfully reached Twitter after entering the online PIN number, but today I can't.
Cookies may be the problem: what appears to be the same thing happened to me, which was fixed when I followed the link given by twitcred$handshake()
in a different browser; i.e., I had been trying in Safari, but then gave it a try once in Chrome and the PIN that I received in Chrome was accepted without a problem in R.