RTweet OAuth Error in Configuration - r

I know literally zero about the R programming language and have come to an impasse where I am truly and utterly stuck.
I've borrowed lots of other peoples code and have been busy debugging placing in additional libraries that are not contained within the help text of the packages to get to a point I feel is tantalisingly close.
That said I simply cannot get R to correctly authorise a twitter search or OAuth Connection.
I run my code, it throws up the following error screen.
Alongside that in the R-Compiler the error message of
twitCred$handshake() To enable the connection, please direct your web
browser to:
http://api.twitter.com/oauth/authorize?oauth_token=r4VjQQAAAAAA4-K7AAABYlMXEws
When complete, record the PIN given to you and provide it here:
registerTwitterOAuth(twitCred) Error: Forbidden
I read that there can be issues with the call back if the weblinks within the code do not read https:// and are left with http:// so that has been amended accordingly to no avail.
My twitter settings are as follows
I followed the advice laid down in this thread but it didn't seem to help or I did something I wasn't supposed to.... StackOverflow Support Article
So here is my code please shout if you can see the obvious to you not to me error or can offer some advice.
## install devtools package if it's not already
if (!requireNamespace("devtools", quietly = TRUE)) {
install.packages("devtools")
}
## install dev version of rtweet from github
devtools::install_github("mkearney/rtweet")
## load rtweet package
library(rtweet)
install.packages("rtweet")
install.packages("RCurl")
install.packages("tm")
install.packages("wordcloud")
install.packages("httpuv")
install.packages(c('ROAuth','RCurl'))
install.packages("rjson")
install.packages("bit64")
install.packages("httr")
require(twitteR)
require(RCurl)
require(tm)
require(wordcloud)
require(httpuv)
require(rtweet)
require(ROAuth)
require(RCurl)
require(rjson)
require(bit64)
require(httr)
## CURL LOGIC ##
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
api_key <- "AAAAAAA"
consumer_key <- "AAAAAAA"
consumerKey <- "AAAAAAA"
consumerSecret <- "AAAAAAA"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
access_token <- "AAAAAAA"
access_token_secret <- "AAAAAAAAAA"
consumerSecret=consumerSecret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL)
twitCred$handshake()
registerTwitterOAuth(twitCred)
##################
The duplications of API keys are meant to be there as I have tried so many connection advice threads and am lost now on the hows whys and wherefores...

You can use the twitteR package for data mining Twitter.
Please find the below code for your reference.
install.packages('twitteR')
install.packages('ROAuth')
install.packages("RCurl")
library(twitteR)
require("ROAuth")
require("RCurl")
download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
t_consumer_key<- 'YOUR CONSUMER KEY'
t_consumer_secret<- 'YOUR CONSUMER SECRET KEY'
t_access_token<- 'YOUR ACCESS TOKEN'
t_access_secret <- 'YOUR ACCESS SECRET'
setup_twitter_oauth(t_consumer_key,t_consumer_secret,t_access_token,t_access_secret)
list <- searchTwitter("iphone")
If you click on the link in below image you will get all the API keys

Related

automatically answer a selection in R [duplicate]

I am using the R package twitteR to post items to Twitter. I put everything inside of a function and it works fine. However, I would like to run the function without being prompted for a response, and I haven't figured out how to do that. Any suggestions?
Here are the bare bones of my function:
doit <- function(<snip>) {
<snip>
# connect to Twitter
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
<snip>
}
When I run the function from the command line, I am prompted for an interactive response.
[1] "Using direct authentication"
Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No
Selection:
I can provide this information directly in a script when the setup_twitter_oauth() function is outside of a function, by entering my response in the following line, much like can be done for other user input functions like readline() or scan().
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
1
However, I haven't been able to get this approach to work when setup_twitter_oauth() is INSIDE of a function.
I would appreciate any suggestions on how to get this to run without requiring user input.
=====
The answer from #NicE below did the trick. I incorporated the options setting in my function as:
doit <- function(<snip>) {
<snip>
# connect to Twitter
origop <- options("httr_oauth_cache")
options(httr_oauth_cache=TRUE)
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
options(httr_oauth_cache=origop)
<snip>
}
You can try setting the httr_oauth_cache option to TRUE:
options(httr_oauth_cache=T)
The twitteR package uses the httr package, on the Token manual page for that package they give tips about caching:
OAuth tokens are cached on disk in a file called .httr-oauth
saved in the current working directory. Caching is enabled if:
The session is interactive, and the user agrees to it, OR
The .httr-oauth file is already present, OR
getOption("httr_oauth_cache") is TRUE
You can suppress caching by setting the httr_oauth_cache option to FALSE.
This works perfectly fine.
install.packages("twitteR", dependencies = T)
install.packages(c('ROAuth','RCurl'))
install.packages("httr")
library(httr)
require('ROAuth')
require('RCurl')
library(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 <- "XXXXXXXXXXXXX"
consumerSecret <- "XXXXXXXXXXXXXXXXXXXXX"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
download.file(url="https://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
twitCred$handshake(cainfo="cacert.pem")
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
I don't know much about it.
But if it's in batch, maybe try this:
doit <- function(<snip>) {
<snip>
# connect to Twitter
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
< echo 1
<snip>
}
Also have you tried posting the 1 outside the function to see if it does the same?
And maybe it will work if you put the 1 under the snip
These are just suggestions, cause i don't know very much about the topic, but it might help though.

Automated httr authentication with twitteR , provide response to interactive prompt in "batch" mode

I am using the R package twitteR to post items to Twitter. I put everything inside of a function and it works fine. However, I would like to run the function without being prompted for a response, and I haven't figured out how to do that. Any suggestions?
Here are the bare bones of my function:
doit <- function(<snip>) {
<snip>
# connect to Twitter
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
<snip>
}
When I run the function from the command line, I am prompted for an interactive response.
[1] "Using direct authentication"
Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No
Selection:
I can provide this information directly in a script when the setup_twitter_oauth() function is outside of a function, by entering my response in the following line, much like can be done for other user input functions like readline() or scan().
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
1
However, I haven't been able to get this approach to work when setup_twitter_oauth() is INSIDE of a function.
I would appreciate any suggestions on how to get this to run without requiring user input.
=====
The answer from #NicE below did the trick. I incorporated the options setting in my function as:
doit <- function(<snip>) {
<snip>
# connect to Twitter
origop <- options("httr_oauth_cache")
options(httr_oauth_cache=TRUE)
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
options(httr_oauth_cache=origop)
<snip>
}
You can try setting the httr_oauth_cache option to TRUE:
options(httr_oauth_cache=T)
The twitteR package uses the httr package, on the Token manual page for that package they give tips about caching:
OAuth tokens are cached on disk in a file called .httr-oauth
saved in the current working directory. Caching is enabled if:
The session is interactive, and the user agrees to it, OR
The .httr-oauth file is already present, OR
getOption("httr_oauth_cache") is TRUE
You can suppress caching by setting the httr_oauth_cache option to FALSE.
This works perfectly fine.
install.packages("twitteR", dependencies = T)
install.packages(c('ROAuth','RCurl'))
install.packages("httr")
library(httr)
require('ROAuth')
require('RCurl')
library(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 <- "XXXXXXXXXXXXX"
consumerSecret <- "XXXXXXXXXXXXXXXXXXXXX"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
download.file(url="https://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
twitCred$handshake(cainfo="cacert.pem")
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
I don't know much about it.
But if it's in batch, maybe try this:
doit <- function(<snip>) {
<snip>
# connect to Twitter
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
< echo 1
<snip>
}
Also have you tried posting the 1 outside the function to see if it does the same?
And maybe it will work if you put the 1 under the snip
These are just suggestions, cause i don't know very much about the topic, but it might help though.

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

OAUTH issues with twitteR package

I am using R and want to use the twitteR package available on CRAN.
I installed the twitteR package using:
install.packages(twitteR)
then loaded the package with:
library(twitteR)
after that I wanted to run the first command to get the latest trends on twitter with:
getTrends(period="weekly")
which showed following error:
Error in getTrends(period = "weekly") :
argument "woeid" is missing, with no default
Also the command:
searchTwitter("#orms")
showed an error, namely:
Error in twInterfaceObj$doAPICall(cmd, params, "GET", ...) :
OAuth authentication is required with Twitter's API v1.1
And also for the command:
userTimeline("informs")
there was an error output:
Error in twInterfaceObj$doAPICall(cmd, params, method, ...) :
OAuth authentication is required with Twitter's API v1.1
What is the reason for that? From my research so far I figured out, it has something to do with oauth. But actually I don't know, what oauth is, and how to configure it, so I can properly use the twitteR package.
Could please anybody provide me some help for this issue??
Thank you very much in advance for your support.
With best regards!!!
1/ You'll need to load ROAuth, which is a dependency of twitteR. See the twitter CRAN docs. http://cran.r-project.org/web/packages/twitteR/twitteR.pdf
Depends: ... ROAuth (>= 0.9.3) ...
2/ You'll need to authenticate as per the below. See pg12 of the twitteR CRAN docs:
## A real example, but using a fictitious consumerkey and consumer
## secret - you’ll need to supply your own
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- "12345pqrst6789ABCD"
consumerSecret <- "abcd1234EFGH5678ijkl0987MNOP6543qrst21"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL)
twitCred$handshake()
registerTwitterOAuth(twitCred)
In general, you should try to search error messages over the CRAN docs of your package - the answer will often be self-contained.

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