Generating OAuth2 token with password grant method

apis
httr

#1

I have an API I need to connect to & generate the .httr-oauth token using the password grant method.

This method requires passing my login name and password (in plain text, why not...), together with my id and secret, to the API.

I know the structure of the expected POST transaction - it is the following (and I have tested it outside of RStudio and it works - so I know the problem is not in my credentials).

https://cruel-and-unusual-api.net/token/?client_secret=client_secret&client_id=client_id&grant_type=password&username=username_with_domain&password=password

But I am unable to recreate this transaction using httr::oauth2.0_token()method. My chief issue is that I am unable to see the URL of my oauth2 call, and so I am guessing blind.

Any help how to tune the process is highly appreciated!


#2

After more than a little headscratching I decided to bypass the .httr-oauth approach entirely and do it the simple way = by using the authorization header directly.

So for the benefit of posterity - when facing unusual API authentication, consider this:

auth_url <- 'cruel-and-unusual-api.net/token/?what-the-heck'
api_url <- 'cruel-and-unusual-api.net/?download-link'

response <- POST(auth_url)

data <- GET(api_url, 
            add_headers(Authorization = paste("Bearer", content(response)$access_token)))

It is crude, but it works and it might be easier to do than getting the httr::oauth2.0_token() work in situation it was not designed to handle.