Raymond_90
Raymond_90

Reputation: 423

How to call API in R with a token

i am experimenting with R and i wonder how could i GET some data from API using library httr or rcurl..

for example with curl in terminal i can do this and it gets me the data i want:

curl -X GET "https://some.webpage.com/api/v1/accounts/self/profile" -H "accept: application/json" -H "token-auth: 72124asfin393483feifefi92835w345"

note: token key is random set of chars

Unfortunately when i try to reproduce this in R i fail, i tried using something like:

> library(httr)
> c <- GET("https://some.webpage.com/api/v1/accounts/self/profile?token- auth=72124asfin393483feifefi92835w345"

or this:

> url = "https://some.webpage.com/api/v1/accounts/self/profile"
> key = "{72124asfin393483feifefi92835w345}"
> a <- GET(url, add_headers(Authorization = paste("Bearer", key, sep = " ")))

Unfortunately when i try this in Rstudio i always get this error:

[1] "'token-auth' has to be provided for authentication."

More info about this particular API call:

API info

This is the documentation

So i guess i obviously do something wrong with the url composition, how would i get this to work with R? I am baffled, i found some documentation on API in R, but nothing to explain how to work with a token. Thanks

Upvotes: 3

Views: 9013

Answers (1)

Raymond_90
Raymond_90

Reputation: 423

Correct solution is what MrFlick said:

Then try a <- GET(url, add_headers("token-auth" = key)). And make sure you don't have the braces in your key string.

Upvotes: 11

Related Questions