Etsy api OAuth2 using httr oauth2.0_token - r

I am trying to use the etsy API which requires both an API key and oauth2 authentication. The example authentication URL that they provide in the docs looks like this:
https://www.etsy.com/oauth/connect?
response_type=code&
redirect_uri=http://localhost:3003/oauth/redirect&
scope=email_r&
client_id=1aa2bb33c44d55eeeeee6fff&
state=superstring&
code_challenge=DSWlW2Abh-cf8CeLL8-g3hQ2WQyYdKyiu83u_s7nRhI&
code_challenge_method=S256
Cobbling together what I can from tutorials for the r httr package I have the following code:
shopID <- '111111' # not real values
etsy_keystring <- 'foobar'
etsy_shared_secret <- 'foobarbaz'
options(httr_oob_default=TRUE)
etsy_endpoint <- oauth_endpoint(authorize = "https://www.etsy.com/oauth/connect",
access = "https://openapi.etsy.com/v3/public/oauth/token")
etsyApp <- oauth_app("etsyApp",
key = etsy_keystring,
secret = etsy_shared_secret,
redirect_uri = "http://localhost:1410")
oauth2.0_token(endpoint = etsy_endpoint,
app = etsyApp,
scope = 'billing_r',
oob_value = "http://localhost:1410")
which opens the browser to the following url:
https://www.etsy.com/oauth/connect?
client_id=foobar&
scope=billing_r&
redirect_uri=http%3A%2F%2Flocalhost%3A1410&
response_type=code
Which etsy gives an error page for. What I think I am missing, is a way to generate PKCE tokens. Is this possible with httr? Or am I missing something completely obvious.

Related

Generate OAuth Token using R

Novice - first time attempting to extract data via an API and using R.
I obtained a API Key and the Secret.
Converted to base64.
Now perplexed as to the next step where the instructions that I have state that I should "Enter the generated base64value in the header and request body and call the token URI as shown below;
[Code]
Authorization: Basic {base64value}
Content-Type: application/x-www-form-urlencoded
POST https://api.destination.com/oauth/token
grant_type=client_credentials
[/Code]
Any insight as to if R can be used to obtain the OAuth Token?
If so, what are the required packages that I need to install?
What are the specific steps?
Currently reading several books on R but thought that someone will be able to provide some insight.
Thanks in advance.
The httr package is a great place to start learning API's in R. If you haven't been lead there already I highly recommend taking the time to check it out.
library(httr)
base64_value <- your_generated_base64string
response <-
POST(url = "https://api.precisely.com/oauth/token",
add_headers(Authorization = paste("Basic", base64_value))
body = list(grant_type = "client_credentials"),
encode = "form")
# we're hoping this is 200
response$status
The following code does not work. A status code of 401 is the result.
After attempting this numerous times, maybe I need to once again regenerate another key and secret. Then, obtain another base64 value. Then, try again. Other options include trying Rcurl or even trying Python.
I assume that the server eventually locks out a base64value after so many unsuccessful attempts.
Appreciate the time/insight.
Any additional insight is appreciated.
library(httr)
base64_value <-
"123456789="
response14 <-
httr::POST (url = "https://api.precisely.com/oauth/token",
httr::add_headers(Authorization = paste("Basic", base64_value)),
body = list(grant_type = "client_credentials"),
encode = "form"
)
Latest iteration.
Error received is regarding timeout.
library(httr)
base64_value <-
"123456789="
response14 <-
httr::POST (url = "https://api.precisely.com/oauth/token",
httr::add_headers(Authorization = paste("Basic", "123456789=")),
body = list(grant_type = "client_credentials"),
encode = "form"
)
Error in curl::curl_fetch_memory(url, handle = handle) :
Timeout was reached: [api.precisely.com] Resolving timed out after 10000 milliseconds

how to retrieve data from a web server using an oauth2 token in R?

I've successfully received an access token from an oauth2.0 request so that I can start obtaining some data from the server. However, I keep getting error 403 on each attempt. APIs are very new to me and I only am entry level in using R so I can't figure out whats wrong with my request. I'm using the crul package currently, but I've tried to make the request with the httr package as well, but I can't get anything through without encountering the 403 error. I have a shiny app which in the end I'd like to be able to refresh with data imported from this other application which actually stores data, but I want to try to pull data to my console locally first so I can understand the basic process of doing so. I will post some of my current attempts.
(x <- HttpClient$new(
url = 'https://us.castoredc.com',
opts = list( exceptions = FALSE),
headers = list())
)
res.token <- x$post('oauth/token',
body = list(client_id = "{id}",
client_secret = "{secret}",
grant_type = 'client_credentials'))
importantStuff <- jsonlite::fromJSON(res$parse("UTF-8"))
token <- paste("Bearer", importantStuff$access_token)
I obtain my token, but the following doesn't seem to work.###
I'm attempting to get the list of study codes so that I can call on them in
further requests to actually get data from a study.
res.studies <- x$get('/api/study',headers = list(Authorization =
token,client_id = "{id}",
client_secret = "{secret}",
grant_type = 'client_credentials'),
body = list(
content_type = 'application/json'))
Their support team gave me the above endpoint to access the content, but I get 403 so I think i'm not using my token correctly?
status: 403
access-control-allow-headers: Authorization
access-control-allow-methods: Get,Post,Options,Patch
I'm the CEO at Castor EDC and although its pretty cool to see a Castor EDC question on here, I apologize for the time you lost over trying to figure this out. Was our support team not able to provide more assistance?
Regardless, I have actually used our API quite a bit in R and we also have an amazing R Engineer in house if you need more help.
Reflecting on your answer, yes, you always need a Study ID to be able to do anything interesting with the API. One thing that could make your life A LOT easier is our R API wrapper, you can find that here: https://github.com/castoredc/castoRedc
With that you would:
remotes::install_github("castoredc/castoRedc")
library(castoRedc)
castor_api <- CastorData$new(key = Sys.getenv("CASTOR_KEY"),
secret = Sys.getenv("CASTOR_SECRET"),
base_url = "https://data.castoredc.com")
example_study_id <- studies[["study_id"]][1]
fields <- castor_api$getFields(example_study_id)
etc.
Hope that makes you life a lot easier in the future.
So, After some investigation, It turns out that you first have to make a request to obtain another id for each Castor study under your username. I will post some example code that worked finally.
req.studyinfo <- httr::GET(url = "us.castoredc.com/api/study"
,httr::add_headers(Authorization = token))
json <- httr::content(req.studyinfo,as = "text")
studies <- fromJSON(json)
Then, this will give you a list of your studies in Castor for which you can obtain the ID that you care about for your endpoints. It will be a list that contains a data frame containing this information.
you use the same format with whatever endpoint you like that is posted in their documentation to retrieve data. Thank you for your observations! I will leave this here in case anyone is employed to develop anything from data used in the Castor EDC. Their documentation was vague to me, so maybe it will help someone in the future.
Example for next step:
req.studydata <- httr::GET("us.castoredc.com/api/study/{study id obtained
from previous step}/data-point-
collection/study",,httr::add_headers(Authorization =
token))
json.data <- httr::content(req.studydata,as = "text")
data <- fromJSON(json.data)
This worked for me, I removed the Sys.getenv() part
library(castoRedc)
castor_api <- CastorData$new(key = "CASTOR_KEY",
secret = "CASTOR_SECRET",
base_url = "https://data.castoredc.com")
example_study_id <- studies[["study_id"]][1]
fields <- castor_api$getFields(example_study_id)

Accessing Twitter Ads API from R using twitteR package?

I am trying to access Twitter Ads API from R, using twitteR package to authenticate, however, I cannot move further, any ideas, did anybody try it using a similar approach? Current error I get as this one (below) My app is whitelisted for ads, and my credentials work through Twurl, using this credential I can without problem access public API, but struggle with Ads one. Thanks!
Error: $ operator is invalid for atomic vectors
library(twitteR)
library(BBmisc)
library(httr)
appname <- “xxxxx”
api_key <- “xxxxxxx”
api_secret <- “xxxxxxxx”
access_token <- “xxxxxxx”
access_secret <- “xxxxxx”
account_id <- “xxxxxxx”
setup_twitter_oauth(api_key,api_secret, access_token, access_secret)
campaign_id <- itostr(18257572,base=36)
reach_campaign.request <- GET(“https://ads-api.twitter.com/2/stats/accounts/xxxxxxx/reach/campaigns?start_time=2018-06-10T00:00:00+01:00Z&campaign_ids=xxxxx&end_time=2018-06-15T00:00:00+01:00Z”,
query = list(granularity = “DAY”,
metric_groups = “ENGAGEMENT”,
placement = “ALL_ON_TWITTER”),
accept_json(),
config(token = access_token))

R - Twitter - fromJSON - get list of tweets

I would like to retrieve a list of tweets from Twitter for a given hashtag using package RJSONIO in R. I think I am pretty close to the solution, but I seem to miss one step.
My code reads as follows (in this example, I use #NBA as a hashtag):
library(httr)
library(RJSONIO)
# 1. Find OAuth settings for twitter:
# https://dev.twitter.com/docs/auth/oauth
oauth_endpoints("twitter")
# Replace key and secret below
myapp <- oauth_app("twitter",
key = "XXXXXXXXXXXXXXX",
secret = "YYYYYYYYYYYYYYYYY"
)
# 3. Get OAuth credentials
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
# 4. Use API
req=GET("https://api.twitter.com/1.1/search/tweets.json?q=%23NBA&src=typd",
config(token = twitter_token))
req <- content(req, as = "text")
response=fromJSON(req)
How can I get the list of tweets from object 'response'?
Eventually, I would like to get something like:
searchTwitter("#NBA", n=5000, lang="en")
Thanks a lot in advance!
The response object should be a list of length two: statuses and metadata. So, for example, to get the text of the first tweet, try:
response$statuses[[1]]$text
However, there are a couple of R packages designed to make just this kind of thing easier: Try streamR for the streaming API, and twitteR for the REST API. The latter has a searchTwitter function exactly as you describe.

Using R to send tweets

I saw a cute demonstration of tweeting from R in a presentation some months ago. The scratch code used by the presenter is here:
http://www.r-bloggers.com/twitter-from-r%E2%80%A6-sure-why-not/
the code is short and sweet:
library("RCurl")
opts <- curlOptions(header = FALSE,
userpwd = "username:password", netrc = FALSE)
tweet <- function(status){
method <- "http://twitter.com/statuses/update.xml?status="
encoded_status <- URLencode(status)
request <- paste(method,encoded_status,sep = "")
postForm(request,.opts = opts)
}
With this function, you can send a tweet simply by using the update function:
tweet("This tweet comes from R! #rstats")
I thought that this could be a useful way of announcing when long jobs are completed. I tried to run this on my machine, and I got some error:
[1] "\n\n Basic authentication is not supported\n\n"
attr(,"Content-Type")
charset
"application/xml" "utf-8"
Warning message:
In postForm(request, .opts = opts) : No inputs passed to form
I'm wondering if there has been some changes on the twitter end of this, that make this code produce this error? I don't know too much about getting R to talk to webpages, so any guidance is much appreciated!!
E
Yes, the basic authentication scheme was disabled on the 16th August 2010.. You'll need to set it up to use OAuth. Unfortunately that is not nearly as simple as using basic authentication
See this twitter wiki page for more information and this StackOverflow question about OAuth for R.
Besides the code you show, there is also a full-blown twitteR package on CRAN you could look at.
The easiest way to tweet in R through the Twitter-API is to use the twitteR Package.
You can set your Twitter-API-APP here: https://apps.twitter.com/
First step is to authenticate:
consumer_key <- "yourcredentials"
consumer_secret <- "yourcredentials"
access_token <- "yourcredentials"
access_secret <- "yourcredentials"
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
And just tweet (limit per day:2400 tweets):
tweet("Hello World")
If twitteR does not work or you simply want to try to build it yourself ...
See here for a demo of how to do your own Twitter authentication and use of the API with help of the httr package.

Resources