Authenticate and API token generation in R/R studio

Hi Team,

I'm doing the API integration with Genesis using R Studio (for R), where my purpose in to extract and load to our database on Genesis data for reporting work.
The problem I'm having is I used how the Python code is setup and used the similar method at the R.

But I'm keep on getting the 400 response code. Actually, I cannot figure out what is going wrong.

Appreciate if I can have bit of a help on this.

library(httr)
library(base64enc)

Set the API endpoint URL

url <- "https://login.mypurecloud.com/oauth/token"

encodedData <- base64enc::base64encode(charToRaw(paste0("myClientID", ":", "myClientSecreat")), newline = FALSE)

Set the request headers

headers <- c(
"Content-Type" = "application/x-www-form-urlencoded",
"Authorization" = paste("Basic ", encodedData)
)

Set the request body

body <- list(
grant_type = "client_credentials"
)

Send the POST request

response <- POST(url, add_headers(headers), body = body, encode = "form")

Hi @ThariP

I actually found the problem here by accident. The paste function seems to use a default separator of a space so there is actually two spaces between "Basic" and encodedData since you are putting a space in manually as well. If you replace paste("Basic ", encodedData) with either of these two lines your code should work.

paste("Basic ", encodedData, sep="")
# or
paste("Basic", encodedData)

Regards,
Declan

1 Like

Hi @Declan_ginty ,

You are legend. Thank you so much.

It is 200. :partying_face:

Just now finished the code thank to your insight.

Have an awesome day.

Thari

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.