[SOLVED] How do I request an API token with httr?

Issue

This Content is from Stack Overflow. Question asked by Mark White

I’m trying to create authenticate into the Letterboxd API using R and the httr package. The Letterboxd docs give instructions, but I am not sure how to put everything together into a URL.

I know the url is:

url <- "https://api.letterboxd.com/api/v0/auth/token"

And then they want my username and password, presumably as JSON, but what I’ll write as a named list since I’m doing this in R:

login_info <- list(
  grant_type = "password",
  username = "myemail@gmail.com",
  password = "extremelysecurepassword"
)

I’ve tried various calls, using GET(), oauth2.0_token(), oauth_endpoint() functions from the httr package.

I feel like I have all the necessary information and am circling around a solution, but I can’t quite nail it.

The docs contain this information:

When generating or refreshing an access token, make a form request to the /auth/token endpoint with Content-Type: application/x-www-form-urlencoded and Accept: application/json headers

(Full text is linked to above)

And I’m not sure how to add that information; in working with APIs through R, I’m used to just sending URLs with UTM parameters, but the inputs they want don’t work here using ? and &.

I’m aware of this related post, but it looks like it relies on having a secret token already. And I don’t seem to be able to generate a secret token inside of the GUI of Letterboxd.com, which is again what I’m used to doing with authentication. I think I need to feed it those sources of information above in login_info to the url, but I don’t quite know how to connect the dots.

How do I authenticate to the Letterboxd API using R?



Solution

You have to pass the parameters as query, as explained in the httr quickstart:

token <- 
GET("https://www.targetvalidation.org/api/latest/public/auth/request_token", 
 query=list(app_name = app_name, secret = secret)
)


This Question was asked in StackOverflow by enricoferrero and Answered by ep82 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?