rtweet - Warning: 32 - Could not authenticate you - r

I notice a few people have encountered issues with rtweet authorization. I followed this vignette which assumes people are familiar with authentication protocols. Newbies such as myself, struggle with this.
vignette("auth", package = "rtweet")`
I stored all keys and tokens in my .Renviron file. This is the code I use to access keys and tokens:
require(rtweet)
require(httpuv)
api_key <- Sys.getenv("TWITTER")
api_secret_key <- Sys.getenv("TWITTER_SECRET")
access_token <- Sys.getenv("ACCESS_TOKEN")
access_token_secret <- Sys.getenv("ACCESS_SECRET")
I then create a token using this code:
token <- create_token(
app = "my_app",
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = access_token,
access_secret = access_token_secret)
Next, I check if a token has been created:
get_token()
I get this response:
<Token>
<oauth_endpoint>
request: https://api.twitter.com/oauth/request_token
authorize: https://api.twitter.com/oauth/authenticate
access: https://api.twitter.com/oauth/access_token
<oauth_app> my_app
key: XXXXXXXXXXXXXXXXXXXX
secret: <hidden>
<credentials> oauth_token, oauth_token_secret
---
I restarted R and run a query as follows:
tweet_data <- search_tweets("#plasticrecycling",
n = 2000,
include_rts = F,
lang = "en")
And end up with this error:
Warning: 32 - Could not authenticate you.
Warning message:
Could not authenticate you.
Can someone please explain why this is happening? A similar issue was reported here https://github.com/ropensci/rtweet/issues/439 but it was closed without a clear answer. I am not sure if create_token() is a once-off thing or must be initiated each time I use R. I notice this added to my .Renviron file:
TWITTER_PAT=/Users/bob/.rtweet_token.rds
Thanks in advance.

Loading the latest development version of rtweet solves the problem I was encountering:
remotes::install_github("rOpenSci/rtweet")
packageVersion("rtweet")
[1] ‘0.7.0.9026’
This version makes it easier to authenticate things.
auth <- rtweet_app(bearer_token = Sys.getenv("BEARER_TOKEN"))
auth_save(auth, "my_app")
auth_list()
[1] "my_app"
auth_as(auth = "my_app")
tweets <- search_tweets("#plasticrecycling",
include_rts = F,
n = 1000,
lang = "en")
One only has to run the rtweet_app() once and then save the output. Every time one restarts R and loads a Twitter script, one simply has to execute auth_as(auth = "your_app")
I was confused by OAuth1.0 versus OAuth2.0 on the Twitter API page. It also did not help reading the wrong online manual pages for a different version of rtweet!
Kudos to llrs for helping me resolve my issue.

Related

TwitteR scraping with R error, fixing ideas?

I'm a bit desperate with my problem as I checked all the existing sources and nothing worked for me.
The issue:
I'm trying to scrape twitter with R in Rstudio, I'm on the very first stage of auth with API keys:
library(twitteR)
library(ROAuth)
library(openssl)
library(httpuv)
library(devtools)
origop <- options("httr_oauth_cache")
options(httr_oauth_cache = TRUE)
api_key <- "*****"
api_secret <- "******"
access_token <- "*****"
access_secret <- "******"
setup_twitter_oauth(api_key, api_secret, access_token, access_secret)
I'm using keys with essential access, v2 access level (which in theory shouldn't be the reason for the bug, but who knows?..)
This code gives the error:
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
What I tried:
1. Restarting the session after installing the packages
2. Fixing httr bug by running:
library(devtools)
devtools::install_version("httr", version="0.6.0", repos="http://cran.us.r-project.org")
3. Fixing possible compatibility issue by running:
devtools::install_github("jrowen/twitteR", ref = "oauth_httr_1_0")
4. running all of the above, restarting R and even restarting the laptop 3 times.
5. Checked my firewall - it's off.
6. Checked my default browser and being logged in in it into twitter - done.
7. Tried re-generating my access keys and re-running all of the above - didn't help.
I don't know what else to try, does anybody have any idea how to fix this issue?
Thanks a million in advance!
Aright, so after some checking here's the solution (if anybody ever needs it):
use library (rtweet)
apply and get approved for elevated access. That was definitely the problem that didn't let me go ahead.

rtweet create_token missing value where TRUE/FALSE needed

So I have been collecting tweets for some months on an external Linux Centos 7.6.1810 server but for some reasons, I can not do it anymore.
So my code is
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_secret = 'xxx'
options(httr_oauth_cache=T)
rtweet::create_token(
app = "appname",
consumer_key = consumer_key,
consumer_secret = consumer_secret,
access_token = access_token,
access_secret = access_secret)
It has been working fine for months but some reasons, now I get the error
Error in if (file == "") stop("'file' must be non-empty string") :
missing value where TRUE/FALSE needed
If i look at the parameters of the function create_token, the only parameter that I haven't put is set_renv but even with that parameter, it does not work.
However, it work fine on my local windows computer... any suggestions?
I had the same issue.
The culprit was the file .rtweet_token.rds which is created every time the authentication is required. Actually, if the file already exists, then a new file is created by appending a number to it (.rtweet_token1.rds , .rtweet_token2.rds, etc).
It happened that I already had 1000 instances of these files and for some reason a new file was not being created.
The solution was to delete all the instances of the file.
Removing the file .rtweet_token.rds did the trick!
You have to delete ALL of the .rtweet_token.rds files which you can do.
If you are in RStudio, you can go to Files, Home, More, and Show Hidden Files. You will then see all of files. Delete them all and then it will work again.

Implement Sign in with Twitter in R language

I have a shiny app that collect tweets from twitter and do some sentiment analysis over them
what i want is to collect tweets through the client's twitter account by make him/her sign in to twitter through my app
so once the client visit my website and want to do some sentiment analysis for certain topic my website will collect tweets related to the topic using the client's Twitter account
I Went through the steps provided by Twitter in this link: https://dev.twitter.com/web/sign-in/implementing
my R code :
library(httr)
library(ROAuth)
credentials <- OAuthFactory$new(consumerKey = "TFJVM92uscmNc7POwlG6YwsgS",
consumerSecret = "YnrYJ9jTxZrW4nLiHu0WrM4tvmFP3eTt6zsEKfEd9rggmpbV2e",
requestURL= "https://api.twitter.com/oauth/request_token",
accessURL= "https://api.twitter.com/oauth/request_token",
needsVerifier=FALSE
)
credentials$handshake(signMethod="HMAC", curl=getCurlHandle())
credentials$OAuthRequest("https://api.twitter.com/oauth/request_token", "POST")
but i get this error:
credentials$handshake(signMethod="HMAC", curl=getCurlHandle())
Error in function (type, msg, asError = TRUE) :
SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
> credentials$OAuthRequest("https://api.twitter.com/oauth/request_token",
"POST")
Error in credentials$OAuthRequest("https://api.twitter.com/oauth/request_token",
:
This OAuth instance has not been verified
Can anyone help me with this
I had a similar error: Error in my_oauth$OAuthRequest(URL = url, params = params, method = "GET", : This OAuth instance has not been verified. The help here might be of use to you, as it was to me. In particular, check out this section of code + comments:
my_oauth$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
# Copy and paste the PIN number (6 digits) on the R console
# Change current folder into a folder where you will save all your tokens
# Now you can save oauth token for use in future sessions with R
Hope this helps.

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.

Resources