0

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 Answers3

1

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
Kent Orr
  • 504
  • 2
  • 10
  • Response [https://api.destination.com/oauth/token] Date: 2021-07-22 09:22 Status: 401 Content-Type: application/json Size: 94 B – BlueTheSky Jul 22 '21 at 09:24
  • Can you provide a link to the original documentation? – Kent Orr Jul 23 '21 at 11:48
  • https://docs.precisely.com/docs/sftw/precisely-apis/main/en-us/webhelp/apis/index.html#Getting%20Started/getting_started.html library(httr) base64_value <- 123456899999= response3 <- httr::POST (url = "https://api.precisely.com/oauth/token/", httr::add_headers(Authorization = paste("Basic", 123456899999=)), body = list(grant_type = "client_credentials"), encode = "form" ) Error in paste("Basic", 123456899999) : argument is missing, with no default – BlueTheSky Jul 30 '21 at 22:07
  • You had two errors in your call. The first is that your url did not include the `https://` portion of the URL. The second is that your base64 string wasn't quoted. It was trying to evaluate or assign because of the `=` at the end. Edited the answer. – Kent Orr Jul 31 '21 at 15:55
  • Any additional insight? – BlueTheSky Aug 02 '21 at 00:17
  • When I run the code you added as an answer, I get a 401 error. That's telling me that the server has received the POST request but the credentials are wrong either because `1234...` is an old token, is typed incorrectly, or is in the wrong format. If you run `httr::content(response14, "text")` you get a more detailed description that tells you you have invalid or missing client credentials. – Kent Orr Aug 03 '21 at 02:27
0

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

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