Twitter OAuth fails with 'Unauthorized' error with R call - r

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)

Related

httr::oauth1.0_token returns HTTP 403

I tried to use httr::oauth1.0_token to authenticate against Twitter, but I got an error:
library(httr)
oauth1.0_token(oauth_endpoints("twitter"),
oauth_app("tw", key = Sys.getenv("Twitter_API"),
secret = Sys.getenv("Twitter_Key")))
# Error in init_oauth1.0(self$endpoint, self$app, permission = self$params$permission, :
# Forbidden (HTTP 403).
As I am new to OAuth authentication, I started reading the Twitter Docs and built the authentication dance from scratch.
After a lot of trial and error, I was eventually successful, so I can rule out that something on the twitter app setting end is wrong (that is the endpoint is set up correctly and especially the callback URL is defined properly, furthermore key and secret are the correct ones).
Before filing a bug report, I wanted to double check that I am using oauth1.0_token correctly. Thus, is this a bug or am I the bug?

API in R & Ravelry

I'm trying to use the Ravelry API to do some data analysis and I'm having difficulties.
I'm modeling my code after this link but I'm afraid this blog may have been written before there were different kinds of permissions on the Ravelry API.
I have a OAuth 1.0a API with a text file with my username, key, and secret
library(httr)
# user_rav.txt contains API username and password
credentials <- readLines("user_rav.txt")
names(credentials) <- c("user","access_key","secret_key")
OpenConnection <- function(credentials){
# Args: login info for the Ravelry API
# Returns oauth token
# Open connection to Ravelry API and return token
reqURL <- "https://www.ravelry.com/oauth/request_token"
accessURL <- "https://www.ravelry.com/oauth/access_token"
authURL <- "https://www.ravelry.com/oauth/authorize"
ravelry.app <- oauth_app("ravelry", key=credentials["access_key"],
secret=credentials["secret_key"])
ravelry.urls <- oauth_endpoint(reqURL, authURL, accessURL)
return(oauth1.0_token(ravelry.urls, ravelry.app))
}
# Quick test of API connection by getting connected user info
TestConnection <- function(ravelry.token) {
# Arg: API token
# Returns name of the user connected with this token
test <- GET("https://api.ravelry.com/current_user.json",
config=config("token"=ravelry.token))
print(content(test)$user$username)
}
ravelry.token <- OpenConnection(credentials)
The last line, "ravelry.token <- OpenConnection(credentials)" produces this error:
Error in init_oauth1.0(self$endpoint, self$app, permission = self$params$permission, :
Internal Server Error (HTTP 500).
I've googled this error and, if you can believe it, there were only four hits. two weren't avaliable and the others were specific to an R package with the API that i'm not using.
I would appreciate any help.
I apologize. It was something as simple as the column heads in the credential file that needed to be edited.

Error in callAPI(query, token)

I had tried to access my FB token using the preliminary code shown by pablo over here :
https://github.com/pablobarbera/Rfacebook
However I get the following error message :
Error in callAPI(query, token) :
An active access token must be used to query information about the current user.
I used this code:
fb_oauth <- fbOAuth(app_id="14025", app_secret="5fdb6ef5776",extended_permissions = TRUE)
save(fb_oauth, file="fb_oauth")
load("fb_oauth")
I understand that I am not having access using my token despite authentication completion. Could someone please help.
Quick fix is just remove type = "application/x-www-form-urlencoded"
from fbOauth.R in RFacebook package. Because now Facebook returns response in JSON format.

TwitteR setup_twitter_oauth() gives an error

I am using twitteR package for the first time.
The setup_twitter_oauth function gives me an error:
[1] "Using browser based authentication"
Error in curl::curl_fetch_memory(url, handle = handle) :
Couldn't connect to server
or
[1] "Using browser based authentication" Error in
init_oauth1.0(self$endpoint, self$app, permission =
self$params$permission, : Bad Request (HTTP 400).
My code:
library(dplyr)
library(purrr)
library(twitteR)
setup_twitter_oauth(getOption("twitter_consumer_key"),
getOption("twitter_consumer_secret"),
getOption("twitter_access_token"),
getOption("twitter_access_token_secret"))
I am using Rstudio and Windows 7 with a proxy.
I believe the problem is that you're literally passing in those strings as arguments to setup_twitter_oath. They should be replaced by access tokens that Twitter gives you when you register your app.
You can assign the actual credentials to objects like
my_key, my_secret, my_access_token, my_access_secret
and then connect like this:
setup_twitter_oauth(my_key, my_secret, my_access_token, my_access_secret)
I noticed that if I try to connect with no credential or fake credentials that I get the same error you were experiencing from setup_twitter_oauth.
Make sure not to quote those objects or they're considered literal character strings.

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