twitteR API for R - r

I want to use Twitter API for R:
I'm using the following R Library-
library(twitteR)
I made my Twitter Developer Account and generate access_token, access_secret, consumer_secret, consumer_key.
consumer_key <- XXXXX
consumer_secret <- XXXXX
access_token <- XXXXX
access_secret <- XXXXX
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
Every time I'm facing the same error:
[1] "Using direct authentication"
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
I read over stack exchange that I need to load the library base64enc. I tried doing that but still gave the same error.
Kindly suggest some movearounds.

Related

Debugging OAuth API Request

I am trying to use the TradeMe API in R, but I keep getting a 401 error when I make the request. Here is my code:
# Install and load needed libraries
library(httr)
# Set keys and secrets
consumer_key <- "my_consumer_key"
consumer_secret <- "my_consumer_secret"
access_token <- "my_access_token"
access_secret <- "my_access_secret"
# Set up the API request
url <- "https://api.tmsandbox.co.nz/v1/MyTradeMe/Watchlist/All.xml"
auth <- paste0("OAuth oauth_consumer_key=", consumer_key,
", oauth_token=", access_token,
", oauth_signature_method=HMAC-SHA1",
", oauth_signature=", consumer_secret, "%26", access_secret)
headers <- c(Authorization = auth)
# Make the request and store the response
response <- GET(url, headers = headers)
# Check the status code of the response
status_code <- response$status_code
print(status_code)
I have double-checked that my keys and secrets are correct and that the OAuth signature method and signature are properly formatted. I have also checked the documentation for the API to make sure that I am using the correct endpoint and that my request is formatted correctly.
However, I am still getting a 401 error. Is there anything else I should be checking or any other potential causes for this error?

Twitter Authentication with R

I am following the latest update on [twitteR homepage][1], and I can't pass the authorization process. I am using Windows 8.1 and the most up to date R packages and R studio/R.
I tried disabling my firewall-- that didn't work.
I tried adding the base64enc package (some people claim that it helped them) but it didn't work.
I need to get this right because it's my first year project for my PhD in Psychology and my adviser will really not be pleased if I can't get this to work.
library("base64enc")
library("twitteR")
library("ROAuth")
api_key <- "XXXXXXXXXXXXXXXXX"
api_secret <- "XXXXXXXXXXXXXXXXX"
access_token <- "XXXXXXXXXXXXXXXXX"
access_secret <- "XXXXXXXXXXXXXXXXX"
setup_twitter_oauth(api_key, api_secret, access_token, access_secret)
This is the output I am getting back:
[1] "Using direct authentication"
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
I believe with the new release in Twitter API an OAuth handshake is necessary for every request you do.
I have extracted tweets recently and my code below using setup_twitter_oauth() works perfectly fine.
First you have to get your api_key and your api_secret as well as your access_token and access_token_secret from your app settings on Twitter. Just click on the “API key” tab to see them.
consumerKey <- "xyz" consumerSecret <- "xyz" accessToken <- "xyz"
accessTokenSecret <- "xyz"
setup_twitter_oauth(consumerKey, consumerSecret, accessToken,
accessTokenSecret)
I figured it out. I needed to use an old version of the httr package.
So on the author Github, some people were saying to use the 0.6.0 version but that's actually causing more problems. By trial and error, I figured out that it's the 1.0.0 version that is necessary. Here is the line of code.
devtools::install_version("httr", version="1.0.0", repos="http://cran.us.r-project.org")
P.S. I changed the keys. Those posted don't work anymore.

TwitteR package failing to grab some tweets due to oauth error (but only some!)

I have the following code to scrape twitter accounts:
library(RColorBrewer)
library(NLP)
library(httr)
library(twitteR)
library(foreign)
library(wordcloud)
library(tm)
library(base64enc)
library(caTools)
library(rpart)
library(rpart.plot)
library(plyr)
access_token<-"" #my credentials are here
access_secret<-""
consumer_key<-""
consumer_secret<-""
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
I then have a character list of users that I apply a function of userTimeline too in order to grab their tweets without having to enter all their usernames in individually.
The code works. I do get OAuth authentication and I am able to grab the vast majority of my users' tweets.
However, there are 7 users that return an OAuth error. They are: #ZestieHorsie, #jjadeceleste, #blondlyfe, #KamiObaro, #valmonella, #KeplinTinyman, and #PromissWright
These users are spaced out unevenly in my list, so I'm getting errors for only these 7 users and being able to grab the rest of them just fine. Note: this isn't an http404 error (there are a couple of those that I've fixed), it's specifically an OAuth error:
try(userTimeline("#jjadeceleste", n=1), silent=F)
Error in twInterfaceObj$doAPICall(cmd, params, method, ...) :
OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
Can anyone help me understand why some of the users get an error and some don't?
Thanks.

Twitter OAuth fails with 'Unauthorized' error with R call

I setup a Twitter app, and gave Read,Write permissions. I noted down the API key and secret. Then, I called setup_twitter_oauth() as follows :
setup_twitter_oauth("key","secret")
[1] "Using browser based authentication"
Error in init_oauth1.0(self$endpoint, self$app,
permission = self$params$permission) :
client error: (401) Unauthorized
As seen, the request is rejected as Unauthorized. What could be the reason? Please let me know if further information is required to answer the question.
You appear to be missing some parameters from your call to setup_twitter_oauth. There are 4 parameters which you need:
consumer_key <- '...'
consumer_secret <- '...'
access_token <- '...'
access_secret <- '...'
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)

R TwitteR package authorization error

I am following the latest update on twitteR homepage, and I can't pass the authorization process.
library(devtools)
install_github("twitteR", username="geoffjentry")
library(twitteR)
api_key <- "XXXXXXXXXXXXXXXXX"
api_secret <- "XXXXXXXXXXXXXXXXX"
access_token <- "XXXXXXXXXXXXXXXXX"
access_secret <- "XXXXXXXXXXXXXXXXX"
setup_twitter_oauth(api_key, api_secret, access_token, access_secret)
This is the output I am getting back:
[1] "Using direct authentication"
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
I have also tried setup_twitter_oauth(api_key, api_secret), and this is the error message:
[1] "Using browser based authentication"
Error in init_oauth1.0(endpoint, app, permission = params$permission) :
client error: (401) Unauthorized
I don't think there are any other options in setup_twitter_oauth. Does anyone else encounter this error?
set callback url to http://127.0.0.1:1410 in app settings in twitter
This error happens when your app is missing the callback url. To solve this issue go to https://apps.twitter.com/ select your application and then go to SETTINGS scroll down to CALLBACK URL and enter ( http://127.0.0.1:1410 ). This should allow you to run browser verification.
Or you can enter the access_token and access_secret in R to trigger local verification.
consumer_key <- " YOUR CONSUMER KEY "
consumer_secret<- " YOUR CONSUMER SECRET "
access_token <- " YOUR ACCESS TOKEN "
access_secret <- " YOUR ACCESS SECRET "
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
I have tried setting call back URL to ( http://127.0.0.1:1410 ), updating all packages related to this package. Nothing solved my problem.
then i installed httk httpuv packages and did the lines below:
consumer_key <- " YOUR CONSUMER KEY "
consumer_secret<- " YOUR CONSUMER SECRET"
setup_twitter_oauth(consumer_key, consumer_secret,
access_token=NULL, access_secret=NULL)
It worked like a beauty.
Doing the above takes to a web page and you manually authorize the app.
While this may not be the solution to the question, it is definitely a workaround to the authentication roadblock.
try install.packages('base64enc') . it worked for me. found it in github discussion.
I had the same problem. Tried all the suggestions I found on the net but in vain.
Probably it has to do with the Callback URL, I had skipped it earlier.
Created a new app, this time included it - http://127.0.0.1:1410 and it worked for me.
Here is the code I used:
library(httr)
library(devtools)
library(twitteR)
library(base64enc)
consumer_key <- 'XXXXXXXXXXXX'
consumer_secret <- 'XXXXXXXXXXXX'
access_token <- 'XXXXXXXXXXXX'
access_secret <- 'XXXXXXXXXXXX'
setup_twitter_oauth(consumer_key , consumer_secret, access_token, access_secret)
tw <- searchTwitter("LFC",n=100,lang="en")
Hope it helps.
I faced the same problem and tried all latest httr download and libraries but still the problem was there. Then I created a new APP in twitter and used API keys and other credentials in the code and now the problem solved.
I was using an APP which i created 8 months back ....regenerating the API credentials may also solve for the existing APP.
Try using this
setup_twitter_oauth(apiKey, apiSecret, access_token = accessToken, access_secret = accessSecret)
I got it fixed by manually generating access token at apps.twitter.com site and pass that as argument to the api which will force to use local authentication rather than browser authentication.
I encountered the same error:
"Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'"
and after trying the different solutions posted here in the stackoverflow, I still did not get the problem solved. I have regenerated my consumer key and consumer secret and supplied it to the following lines in my R script:
consumer_key <- 'XXconsumer_keyXX'
consumer_secret <- 'XXconsumer_secretXX'
access_token <- 'XXaccess_tokenXX'
access_secret <- 'XXaccess_secretXX'
setup_twitter_oauth(consumer_key , consumer_secret, access_token, access_secret)
What I did to get it right and get the OAuth authentication handshake is to supply the twitter supplied consumer key, consumer secret, access token, access secret value directly to the 5th line above, that is,
setup_twitter_oauth("xxconsumer_key_xx", "xxconsumer_secretxx", "xxaccess_tokenxx", "xxaccess_secretxx")
This works for me and hope it will for you.
I also had this issue and went through everything posted here to no avail. I finally looked at Windows Firewall and realized I had not made an exception for Rstudio. Everything works now!

Resources