Authenticate using httr package when Making API Requests
Asked Answered
M

2

6

I'm learning how to fetch data using an API in R. I understand that the aim of httr is to provide a wrapper for the curl package. The documentation I'm following so that I make requests to the API has the following HTTP request format. This code below will be used to generate a token

curl -s \
     -d "client_id=clientid” \
     -d "username=user” \
     -d "password=pwd” \
     -d "grant_type=password" \
     -d "scope=openid email" \
     "https://auth.com/token"

Afterward, I'll use the token to now communicate with the API using this request

curl --header "Content-Type: application/json" \
     --header "Accept: application/+json" \
     --header "Authorization: Bearer token_goes_here“ \
     --request GET \
     --url "https://api-sitename.org/sections?parent_id=0"

Initially, I run these two requests in a terminal and they were successful, I got a response in JSON format. My question is, how do I run these requests in an R script such that I get a responses and they're it's stored in R studio global environment? My goal is to finally load the dataset from the API to the Rstudio working environment. T

Mathre answered 12/5, 2020 at 10:11 Comment(1)
were you able to succeed ? I had similar issue, where it works in terminal but fails in R. Some where the way I am passing it is problematicSnuggery
G
3

Here is something to get you started:

library(httr)
resp <- POST("https://auth.com/token", 
    body=list(client_id="clientid",
        username="user",
        password="pwd",
        grant_type="password",
        scope="openid email")
)
#parse for auth token here
content(resp, "text")

get_resp <- GET("https://api-sitename.org/sections?parent_id=0",
    add_headers("Content-Type"="application/json",
        Accept="application/+json",
        "Authorization"=paste("Bearer", token))
Gasbag answered 12/5, 2020 at 23:32 Comment(0)
S
1

I was able to successfully get my API call in R by replacing the content in header to body. Here is my code

#' Th base url
base_url <- "your/url/endpoint/for/token"
#  base64 encoded client id, my end-point requires to encone the client id to base64
c_id <- RCurl::base64(txt = "clinetid:sceret", mode = "character")
#' headers
headers <- httr::add_headers(
  "Authorization" = paste("Basic",c_id, sep = " ")
)
# move everything else to the body. grant_type and password were requested by the endpoint
body <- list(
  username = "your username",
  password = "your password",
  grant_type = "password",
  scope = "read"
)

#' post call to get the token
httr::POST(
  url  = base_url,
  body = body,
  config  = headers,
  httr::accept_json()
)

When I had the user name and password in the body, I received 400 and 403 errors. Once I moved them o the body received 200 status and the token was successfully retrieved. If you can provide what you tried in R, can help you troubleshoot.

Snuggery answered 22/1, 2021 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.