twitteR throws Forbidden error after entering twitter API PIN - r

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.

Related

Twitter Authentication in R

Trying to get Twitter authentication to work in R (for analyzing tweets), but RStudio does not respond to the PIN + get error messages.
I use Mac OS X 10.6.8 and RStudio Version 0.98.1091
Here's the code I'm using (incl. placeholders):
install.packages("twitteR")
library(twitteR)
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="my_folder/my_subfolder/http://curl.haxx.se/ca/cacert.pem")
require(twitteR)
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL = "https://api.twitter.com/oauth/access_token"
authURL = "https://api.twitter.com/oauth/authorize"
consumerKey = "xxxx"
consumerSecret = "yyyyy"
Cred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=requestURL,
accessURL=accessURL,
authURL=authURL)
Cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )
I get this:
"To enable the connection, please direct your web browser to:
https://api.twitter.com/oauth/authorize?oauth_token=bWNevwJNJUPEzKLJwHQsK2uRLN4At5LO
When complete, record the PIN given to you and provide it here: registerTwitterOAuth(Cred)
Error: Authorization Required"
When I enter the PIN in the console I only get this:
>1234567
[1] 1234567
I then try to run
registerTwitterOAuth(Cred), but get this in the console:
Error in registerTwitterOAuth(Cred) :
oauth has not completed its handshake
Not sure what I am doing wrong or how I can obtain authentication/authorization.
Very thankful for any help on this.
try this
library(httk)
library(httpuv)
#browser based authentication
consumer_key <- "xxxxxxxxxxxxxxxxxxxx"
consumer_secret<- "xxxxxxxxxxxxxxxxxxxxxxxxx"
setup_twitter_oauth(consumer_key, consumer_secret, access_token=NULL,access_secret=NULL)
I used the following for Twitter authentication in Windows OS, and it works fine. Maybe it gives some direction!
library(RCurl)
library(twitteR)
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
consumerKey <- "insert your consumer key here"
consumerSecret <- "insert your consumer secret here"
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 finally figured it out! The console won't let me copy the url after running the code, so I have to type the url with the unique token by hand. I wasn't aware of this. It's a little cumbersome, but at least it works. I'm running into other problems, which I read elsewhere on StackOverflow, might be a problem with Mac's operative system, so this might be linked to it as well. RStudio doesn't seem to jive to well with Mac.
Thanks for all the help!
Make sure that in the Application Settings of Twitter Developers you entered:
Callback URL: http://127.0.0.1:1410
Sign in with Twitter: Yes

0Auth handshake failed in R

I am attempting to set up a tweet sentiment analysis tool in R, however I keep getting errors in relation to 0Auth not completing it's handshake.
it's using a tutorial that I found online, however I'm new to R and coding in general so I'm stumped.
Any light that can be shed on this would be much appreciated:
install.packages("twitteR")
install.packages("plyr")
install.packages("stringr")
install.packages("ggplot2")
install.packages("streamR")
library(twitteR)
library(ROAuth)
library(plyr)
library(stringr)
library(ggplot2)
## Windows users need to get this file
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
requestURL <- "https://api.twitter.com/oauth/request_token"
accessURL = "https://api.twitter.com/oauth/access_token"
authURL = "https://api.twitter.com/oauth/authorize"
consumerKey = "conkey code"
consumerSecret = "consec code"
Cred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=requestURL,
accessURL=accessURL,
authURL=authURL)
Cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )
1340490
load("twitter authentication.Rdata")
registerTwitterOAuth(Cred)
Have you tried looking at the creators github page:(https://github.com/geoffjentry/twitteR/blob/master/README.md) It's an easier way to do it. You will also need to install Rtools if you are going to do it this way. Here is a youtube link that shows you how to install Rtools(https://www.youtube.com/watch?v=enPPMHr5SrM) it is in Chinese but you can still follow what this guy is doing. I hope this helps.

Rstudio to Twitter handshake giving Error: Unauthorized via twitterR

I am trying to authenticate Rstudio to Twittter using twitteR.
I only error after the handshake.
My code is:
#install(rcurl, ROAuth, twitteR)
#load(rcurl, ROAuth, 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 <- "key"
consumerSecret <- "secret key"
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()
I get the handshake and it send me to the auth page for Twitter. I authorize and get a pin.
I enter the pin in this code:
registerTwitterOAuth(pin number here)
Then I am returned with a "Error: Unauthorized".
I added a new Twitter application account and got a different set of customer keys, but still got the same.
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()
The last version for twitteR is 1.1.7 => http://cran.r-project.org/web/packages/twitteR/index.html
Following the below steps made my code work after trying out the suggestions from every possible resource over the net!!!! I am using windows 7, 64 bit and RStudio for the code below
Make sure you've got all appropriate packages installed - ROAuth, twitteR, RCurl; Usually the dependencies indicated on the CRAN page will point you to the missing packages if any
Set SSL certs globally as shown in the code; I am not an expert as to why this is required but it > helped me get my code through Use download.file command to download the cacert.pem file Make sure your twitter app has full read write access and not just the read only access which is the default
Final Code to link with Twitter
Get appropriate libraries
library(twitteR)
library(ROAuth)
library(RCurl)
Set SSL certs globally
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
Make sure the url's are https and not http in case you have copied the code from somewhere
reqURL <- "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=reqURL,
accessURL=accessURL,
authURL=authURL)
#twitCred <- getTwitterOAuth(consumer_key= consumerKey, consumer_secret=consumerSecret)
twitCred$handshake()
At this point you need to paste the URL into the browser which will open a twitter page with your > application - you need to get the PIN from there and paste it in the R Console
registerTwitterOAuth(twitCred)
TESTTHE CODE: Now you can test the code
testSearch = searchTwitter("#food", n = 10, cainfo="cacert.pem")
Moditweets <- searchTwitter("#Narendra Modi", n = 100)
Following worked for me, windows 8, note that authentication must be done in cmd, it cant happen via Rstudio IDE, you want to authenticate R as language
#Open admin shell: In the Start Search box, type cmd, and then press CTRL+SHIFT+ENTER.
#Goto C:/...R../bin/x64, run ./r.exe
install.packages("twitteR") #Takes some time for mirrors to pop-up
library(twitteR)
library(ROAuth)
library(RCurl)
download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
requestURL <- ...
accessURL <- ...
authURL <- ...
consumerKey <- ...
consumerSecret <- ...
twitCred <- OAuthFactory$new(consumerKey=consumerKey, consumerSecret=consumerSecret,
requestURL=requestURL, accessURL=accessURL, authURL=authURL)
twitCred$handshake(cainfo="cacert.pem")
registerTwitterOAuth(twitCred)
save(list="twitCred", file="twitteR_credentials")
## Now to get some tweets, place the files cacert.perm and
twitteR_credentials from bin folder to the the current working directory of Rstudio
library (twitteR)
load("twitteR_credentials")
registerTwitterOAuth(twitCred)
searchTwitter('#Obama', cainfo="cacert.pem", n=5, locale = 'en')

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.
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

Unauthorized error with ROAuth

I am using the streamR package to pull tweets from the Twitter Streaming API. This was working fine till recently. Now - I am getting the error whenever I do a handshake.
> library(ROAuth)
> reqURL <- "https://api.twitter.com/oauth/request_token"
> accessURL <- "https://api.twitter.com/oauth/access_token"
> authURL <- "https://api.twitter.com/oauth/authorize"
> consumerKey <- "<myconsumerkey>"
> consumerSecret <- "myconsumersecret>"
> my_oauth <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
> my_oauth$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
Error: Unauthorized
I have tried recreating a new application on dev.twitter.com and I still get the same error. I have tried changing the callback URL and the access levels - but no use. I have also tried using the master branch of ROAuth from github. Any idea where I am going wrong? I am using R 3.0.1
I had the same problem. The following sequence of commands solved the problem:
consumerKey <- ...
consumerSecret <- ...
twitCred <- getTwitterOAuth(consumer_key= consumerKey, consumer_secret=consumerSecret)
searchTwitter()
Notice I didn't use registerTwitterOAuth(twitCred). I think if you wanna save and later load twitCred you have to use registerTwitterOAuth(twitCred) after loading.
I hope this helps.
P.S. Since I am using a Mac, I didn't use the cacert.pem file. I think it is just required for Windows.

Resources