OAuth2 Not Working

Hi All,

I am using HTTP Request activity to call a REST API which uses OAuth2 authorization. I was able to get the access token successfully and I extracted necessary value from returned JSON response by using Deserialize JSON activity. Assume extracted value is in a variable called acsTknRspns (Type: JObject).

When I provide set this value to the OAuth2Token value of my next HTTP Request, the workflow ends with code 401, unauthorized access.

image
But I overcame this issue as follow.

I set the Headers of the next HTTP Request like,

Bearer + acsTknRspns(“access_token”).ToString.
image
By this way, I can make a successful request.

Am I using the OAuth2 wrong? If so how to set access token value in OAuth2Token property?

Can you guide me regarding this one Bearer + acsTknRspns(“access_token”).ToString. where have you used this argument and why have you given the default vale as this?

Hi @Ankita121,

As mentioned I am passing this as argument to Headers of the HTTP Request activity. Also Bearer + Access Token Value is the default way to set headers in OAuth 2.0 authentication.

You can include your access token in your POST body if the service accepts access token in body.

Hi nsiva,

I can see that you were able to get the access token. I’m not able to get the access token using the http request. Could you please help me on this or share any sample xaml if available

Hi @nsiva
I already did automations using API with token needed and did like you did in headers and works, not in the OAuth2.

1 Like

HI @karthiga.nandakumar,

Welcome to UiPath forum. Use http request activity and provide details as below.

Set your EndPoint property with your access token url.

Often access token HTTP request body takes a following structure.

{
“grant_type”:“password”,
“scope”:"*",
“client_id”:“Your client ID”,
“client_secret”:“Your client secret”,
“username”:“username”,
“password”:“password”
}

Client ID, Client secret will be available from access token provider. You just need to input this value into Body property of above activity.

But since we need to input string within double quotes in UiPath, you need to parse this as follows.

“{”“grant_type”":"“password”","“scope”":""*"","“client_id”":"“Your client ID”","“client_secret”":"“Your client secret”","“username”":"“username”","“password”":"“password”"}"

(Try giving this in single line as above)

Please note that you need set BodyFormat property as application/json

Now you will be able to make the request successfully.

Hi nsiva,

Thank you for responding. I do not have the client ID and client secret. I was able to get the access token from postman by using grant type password and using the password and username.

this issue resolved ?? am also facing same issue

Hi @nsiva, while making this first call to REST API to get access token, was the calling method GET or POST. Also, did you pass anything in header or body during this call?

Hi @kapil775,

REST API calls to get access token was a POST request since we need to provide details such as client id, client secret etc. Also in my case, I did not pass anything in my header.

In general, when making HTTP requests, we set headers with Content-Type ==> application/json (for a JSON body). We don’t need to specify this here since we configure that using BodyFormat property.