0
votes

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.

3

3 Answers

1
votes

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
0
votes

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"
             )
0
votes

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