API in R & Ravelry - r

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.

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
)

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues

I am trying to use the TumblR package in R to set up the Oauth Authentication to Retrieve a user's dashboard using the second example in
tumblR documentation
However I get the following error, it seems that using twitter others have been able to use a different function to get around this, but I am not finding the same function available for Tumblr.
See twitter package for R authentication: error 401
My code
consumer_key <- OKey
consumer_secret <- SKey
appname <- App_name
tokenURL <- 'http://www.tumblr.com/oauth/request_token'
accessTokenURL <- 'http://www.tumblr.com/oauth/acces_token'
authorizeURL <- 'http://www.tumblr.com/oauth/authorize'
app <- oauth_app(appname , consumer_key, consumer_secret)
endpoint <- oauth_endpoint(tokenURL, authorizeURL, accessTokenURL)
token <- oauth1.0_token(endpoint, app)
The error I am receiving is the following.
Error in init_oauth1.0(self$endpoint, self$app, permission =
self$params$permission, : Unauthorized (HTTP 401)
I am using R version 3.4.0 and Rstudio Version 1.1.463
Reply from the package maintainer.
I'm sorry for the delay in the reply, I'm currently out for work.
The problem you reported depends on the change in the http protocol. The API URLs are now passed to https.
You can use
tokenURL <- 'https://www.tumblr.com/oauth/request_token'
accessTokenURL <- 'https://www.tumblr.com/oauth/acces_token'
authorizeURL <- 'https://www.tumblr.com/oauth/authorize'
I realized it only thanks to your report.
During the Christmas holidays, I plan to arrange the package.
If you have other evidence, let me know.

Trouble receiving data from Twitter in R

I want to get data from a tweet.
But these are the errors.
Please help me get data?
by code
install.packages("rtweet")
library(rtweet)
# plotting and pipes - tidyverse!
library(ggplot2)
library(dplyr)
# text mining library
library(tidytext)
# whatever name you assigned to your created app
appname <- "**********"
## api key (example below is not a real key)
key <- "**************"
## api secret (example below is not a real key)
secret <- "***********"
twitter_token <- create_token(
app = appname,
consumer_key = key,
consumer_secret = secret)
rstats_tweets <- search_tweets(q = "#rstats",
n = 500)
Error in check_twitter_oauth (): OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth () '
Error in curl :: curl_fetch_memory (url, handle = handle):
Timeout was reached: Connection timed out after 10,000 milliseconds
The documentation claims:
API authorization
The first time you make an API request—e.g., search_tweets(), stream_tweets(), get_followers()—a browser window will open.
Log in to your Twitter account.
Agree/authorize the rtweet application.
And that’s it!
However, this did not work for me.
I had to follow the rtweet tutorial how obtaining and using access tokens:
# install from CRAN
install.packages("rtweet")
# load rtweet
library(rtweet)
I also had to install the httpuv package. This is not mentioned in the tutorial but you get a corresponding error message in R.
Error in oauth_listener(authorize_url, is_interactive) :
httpuv package required to capture OAuth credentials.
install.packages("httpuv")
Then Create your Twitter app with the correct Callback URL on apps.twiter.com:
To create a Twitter app, navigate to apps.twitter.com and create a new app by providing a Name,
Description, and Website of your choosing (example screenshot provided
below).
Important In the Callback URL field, make sure to enter the following: http://127.0.0.1:1410
Check yes if you agree and then click “Create your Twitter application”.
## whatever name you assigned to your created app
appname <- "rtweet_token"
## api key (example below is not a real key)
key <- "XYznzPFOFZR2a39FwWKN1Jp41"
## api secret (example below is not a real key)
secret <- "CtkGEWmSevZqJuKl6HHrBxbCybxI1xGLqrD5ynPd9jG0SoHZbD"
## create token named "twitter_token"
twitter_token <- create_token(
app = appname,
consumer_key = key,
consumer_secret = secret)
then, finally, a browser window opened after this code is executed (something like the following):
http://localhost:1410/?oauth_token=asdfasdfasdfaf5naw&oauth_verifier=dffasdfsdfsdfsdfsdfsk9jIQxDG6
showing the message:
Authentication complete. Please close this page and return to R.
and now you should be good to go.
See also:
https://developer.twitter.com/en/docs/basics/authentication/overview/oauth

Quickbooks Online API Oauth2.0 using httr

I am trying to access the QuickBooks online API via R. Here is what I have so far:
library(httr)
library(httpuv)
endPoint <- oauth_endpoint(request = NULL,
authorize = "https://appcenter.intuit.com/connect/oauth2",
access = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer")
App <- oauth_app("Untitled",
key = "xxxx",
secret = "xxxxx",
redirect_uri = "http://localhost:1410/")
QBOtoken <- oauth2.0_token(endpoint = endPoint,
app = App,
scope = "com.intuit.quickbooks.accounting",
type = "code",
cache = T)
GET("https://sandbox-quickbooks.api.intuit.com/v3/company/193514718345164/query?query=Select * from Payment", config(token = QBOtoken))
When run the code above through QBOtoken, I go through the whole Oauth2.0 "dance" and get the response:
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
However, when I execute the GET command, it returns:
Error in self$credentials$access_token :
$ operator is invalid for atomic vectors
The .httr-oauth file that is generated is filled with 284 rows of 32 characters. Is that normal?
I have connected to the API via Postman and it generates an access token allowing me to execute queries like the GET request in my code. There is also an Oauth 2.0 playground on Intuit Developer. Somehow, in R, I am not getting the access token and refresh token.
Another concern, is that I am currently trying to connect to a development environment. For QuickBooks Online, the redirect URL can be the localhost in the development environment, but if I want to connect to the production company(my company) data I will need a https redirect URL. Will this be possible through R?
My ultimate goal for this project is for the script to automatically run on a nightly basis, connect to the API, and ETL the data into a Relational Database for reporting/analytics purposes. Any help would be greatly appreciated!
I did get this "functional," but it is by no means good code. Some of this, like storing tokens in a csv, is not best practice, just a band-aid to test the code.
I created a tokens.csv file in the structure:
"RefreshToken","AccessToken"
"<RefreshToken>","<AccessToken>"
Below is my script that retrieved customer data from the QBO api sandbox.
library(httr)
library(httpuv)
library(curl)
library(jsonlite)
library(base64enc)
#Client ID and Client Secret were retrieved from the online explorer
clientID <- "<ClientID>"
clientSecret <- "<ClientSecret>"
scope <- "com.intuit.quickbooks.accounting"
tokens <- read.csv("tokens.csv")
RefreshToken <- as.character(tokens$RefreshToken[1])
AccessToken <- as.character(tokens$AccessToken[1])
authorize <- base64enc::base64encode(charToRaw(paste0(clientID,":",clientSecret)))
oauth_refresh <- httr::POST("https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer",
add_headers('Content-Type'= "application/x-www-form-urlencoded",
'Accept'= 'application/json',
'Authorization'= paste0('Basic ',authorize)
),
body = list('grant_type'='refresh_token',
'refresh_token'=RefreshToken),
encode = "form")
oaJSON <- fromJSON(content(oauth_refresh, as = "text"))
RefreshToken <- oaJSON[["refresh_token"]][1]
AccessToken <- oaJSON[["access_token"]][1]
tokens <- as.data.frame(list('RefreshToken'=RefreshToken,'AccessToken'=AccessToken))
write.csv(tokens,file = "tokens.csv", row.names = F)
datas <- httr::GET("https://sandbox-quickbooks.api.intuit.com/v3/company/<ID>/query?query=SELECT%20%2a%20FROM%20Customer",
accept_json(),
add_headers('Authorization'= paste0("Bearer ",AccessToken))
)
#datas$status_code
j_son <- content(datas, as = "text")
customers <- fromJSON(j_son)
customer_df <- customers$QueryResponse$Customer
Hopefully this gets the ball rolling correctly for you. Let me know if you have any feedback!

Resources