Legacy Dev Forum Posts

 View Only

Sign Up

Authenticate and API token generation in R/R studio

  • 1.  Authenticate and API token generation in R/R studio

    Posted 06-05-2025 18:30

    ThariP | 2024-04-28 23:20:38 UTC | #1

    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( granttype = "clientcredentials" )

    Send the POST request

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


    Declan_ginty | 2024-04-29 11:09:18 UTC | #2

    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


    ThariP | 2024-04-29 14:42:38 UTC | #3

    Hi @Declan_ginty ,

    You are legend. Thank you so much.

    It is 200. 🥳

    Just now finished the code thank to your insight.

    Have an awesome day.

    Thari


    system | 2024-05-29 14:42:49 UTC | #4

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


    This post was migrated from the old Developer Forum.

    ref: 25956