Error resource owner password credential in R API

I am using r to retrieve the token through the httr, but I got a 400 errorcode (bad request) . Here is my code:

library(httr)
library(jsonlite)

r <- POST("https://XXXXXX.com/authservice/connect/token",
          config = list(),
          body = list(
            grant_type    = "RESOURCE_OWNER_PASSWORD_CREDENTIALS",
            client_id     = "ResourceName",
            client_secret = "Resourcepwd",
            scope         = "xxxGroup"
          ),
          encode = "form"
)
warn_for_status(r)

I tested the same setting in SoapUI, and I got the token

Can someone help to take a look? Any input is greatly appreciated.

Hi Williaml, can you also kindly look at my another questions?

Question on Oauth2 Resource Owner Password Credential Grant in R httr

I use Oauth2 authorization to get the token for REST API. I tested the setting in SoapUI and it is working with the Resource Owner Password Credential Grant, as shown below.

Now, I want to retrieve the token through R httr library, but I don’t know how to set the resource owner name and password in the R code. Here is my Code

library(httr)
library(jsonlite)

r <- POST("https://XXXXXXX.com/authservice/connect/token",
          config = list(),
          body = list(
            
            # grant_type      = "RESOURCE_OWNER_PASSWORD_CREDENTIALS",
            grant_type        = "resource_owner_credentials",
            
            resourceOwnerName         = "ResourceName",
            resourceOwnerPassword     = "Resourcepwd",
            
            # resource_Owner_Name     = "ResourceName",
            # resource_Owner_Password = "Resourcepwd",
            
            # resourceOwner_Name      = "ResourceName",
            # resourceOwner_Password  = "Resourcepwd",
            
            # resource_OwnerName      = "ResourceName",
            # resource_OwnerPassword  = "Resourcepwd",
            
            # resource_Name           = "ResourceName",
            # resource_Password       = "Resourcepwd",
            
            # resource_id             = "ResourceName",
            # resource_secret         = "Resourcepwd",
            
            client_id            = "ClientIDname",
            client_secret        = "Clientpassword",
            
            scope                = "xxxGroup"
          ),
          encode = "form"
)
warn_for_status(r)
content(r)
status_code(r)

I got a 400 error ("unsupported_grant_type" and bad request). I am guessing there may be something wrong with the syntax on the resource onwer name and password, so I also tried all the possible combinations that I commented in above code but they all failed.

My question is, are the parameter names for both resource owner name and password predefined by the API document, or they are the fixed names in R-httr, regardless what token authorization service?

Can someone help to take a look at? Any input is greatly appreciate!

This question also can be found here:

Looks like you have a solution already at SO.

Otherwise, you could have tried using query() perhaps.

1 Like

Hi Williaml, thank you very much. Yes, I got the solution.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.