httr icon indicating copy to clipboard operation
httr copied to clipboard

Question: how to pass additional vars within authenticate?

Open fahadshery opened this issue 6 years ago • 3 comments

Probably not an issue but I have asked the question here

I have a weird api setting an additional static variable in the call grant_type=password. I am not sure how to pass it within authenticate method? any pointers would be helpful.

Ps I can authenticate using python without any issues

fahadshery avatar Nov 08 '19 16:11 fahadshery

Hi,

From the API DOC your API is supporting Oauth2 authentication using Resource Owner Password Credentials Grant that uses grant_type = "password".

For Oauth2 API you would usually use httr::oauth2.0_token but currently, httr Oauht2 flow does not support this type of grant.

You would need to do it yourself.

From the doc (I did not try), you would need to use a POST with a body containing your parameter.

This should work:

username <- "my_username"
password <- "my_psw"
base_url = "https://api.checkbox.com/v1/my_account/oauth2/token"

response <- POST(url=base_url,
                 # pass data to the POST request
                 body = list(
                   username = username,
                   password = password,
                   grant_type = "password"
                 ),
                 # application/x-www-form-urlencoded
                 encode = "form",
                 verbose())

Can you check ?

cderv avatar Nov 08 '19 19:11 cderv

related to #564 about the same type of grant

cderv avatar Nov 08 '19 19:11 cderv

Thanks! Not sure if it helps anyone but below yielded success with base_url being Authorize Endpoint:

response <- POST(url=base_url,
                 accept_json(),
                 body = list(
                   username = username,
                   password = password,
                   grant_type = "password"
                 ),
                 encode = "form",
                 verbose())

ponnet avatar Apr 21 '20 18:04 ponnet

httr has been superseded in favour of httr2, so is no longer under active development. If this problem is still important to you in httr2, I'd suggest filing an issue offer there 😄 — but httr2 provides much stronger OAuth support and what you want is likely to be https://httr2.r-lib.org/dev/reference/req_oauth_password.html. Thanks for using httr!

hadley avatar Oct 31 '23 20:10 hadley