Debugging OAuth API Request - r

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?

Related

LinkedIn API error of redirect uri from httr

I'm trying to access LinkedIn's API using R and the httr package.
When I execute the last oauth2.0_token() function, in order to gain an authorization token, I get the following error from LinkedIn: "The redirect_uri does not match the registered value".
I've set my redirected url on the LinkedIn Developer site to http://my_app_54321
Does anyone know what the solution is?
# Packages
library(httr)
# Client info
clientid <- "my_id"
secret <- "my_secret"
# App
app <- oauth_app(appname = "app name", key = clientid, secret = secret)
# Endpoints
endpoint <- oauth_endpoint(base_url = "https://www.linkedin.com/uas/oauth2",
authorize = "authorization", access = "accessToken")
# Access token
token <- oauth2.0_token(endpoint = endpoint, app = app)
token
Changing my redirected url to the following solved it since I just needed the application to run locally.
http://localhost:1410/
The comments from the GitHub of httr package pointed in this direction:
https://github.com/r-lib/httr/blob/master/demo/oauth2-linkedin.r

(R) Generating an oauth 1.0 token for Flickr using httr

I'm trying to generate an access token for my Flickr app in httr. My goal is to download images from certain geographical co-ordinates. To my understanding this isn't something the FlickrAPI package can do, so I've decided to use httr.
Running the below code brings up the authorization page but when I submit the authorization I get a localhost error (localhost unexpectedly closed the connection).
What am I forgetting here? I haven't set a callback URL, if that's relevant.
library(httr)
setwd("G:\\Project\\R Code\\")
api_key <- "<TOKEN>"
secret <- "<SECRET>"
flickr.app <- oauth_app("App Name", api_key, secret)
flickr.endpoint <- oauth_endpoint(
request = "https://www.flickr.com/services/oauth/request_token",
authorize = "https://www.flickr.com/services/oauth/authorize",
access = "https://www.flickr.com/services/oauth/access_token"
)
token <- oauth1.0_token(
flickr.endpoint,
flickr.app,
cache = FALSE
)

OAuth2 authentication with R using httr

I am trying to read some data from the Polar Accesslink API. I have read the documentation and examples (I'm not that familiar with Python) as well as httr examples/demos of Oauth2.0:
https://www.polar.com/accesslink-api/#polar-accesslink-api
https://github.com/polarofficial/accesslink-example-python
Here is my code:
library(httr)
client_id <- rstudioapi::askForPassword()
client_secret <- rstudioapi::askForPassword()
# redirect_uri determined in registration to http://localhost:1410/
app <- oauth_app(appname = "polar_app",
key = client_id,
secret = client_secret)
endpoint <- oauth_endpoint(
request = NULL,
authorize = "https://flow.polar.com/oauth2/authorization",
access = "https://polarremote.com/v2/oauth2/token")
token <- oauth2.0_token(endpoint = endpoint,
app = app,
scope = "accesslink.read_all", # tested also without scope
use_basic_auth = T # tested both T and F
)
Last part of the code (token) produces the following error:
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params, :
Bad Request (HTTP 400). Failed to get an access token.
It opens browser with url localhost:1410/?state={STATE}&code={CODE} and says:
"Authentication complete. Please close this page and return to R."
Based on the API documentation and github Python examples, I can't figure out if I need to set some values for user_params or query_authorize_extra in oauth2.0_token.
Having the same issue. Sent a message off to support and they came back with making sure that you include header "Content-Type: application/x-www-form-urlencoded" and that the body gets sent as plain/raw text and contains "grant_type=authorization_code&code=<AUTHORIZATION_CODE>".
I have not tested this out but hoping it helps you out if you have not already found a solution

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.

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