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?

1 Like

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

1 Like

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.

Hi nsiva,

I am trying to get the access token by using HTTP Request which uses OAuth2 auhorization.
Can you share xaml file or guide me for the same.

As I am not sure what needs to be filled in Parameter name OAuth2Token of Http Request.

Hi @ronak83garg,

I am little bit confused on your question. Are you having trouble in getting your access token? If that is the case, simply you can get it by using HTTP Request Activity with relevant parameters in body with no value set for OAuth2token (since this is what we are trying to get). Then you can deserialize server response and extract the field containing access token.

In my case my server returned a JSON response.
{
“accessToken”:“ABCDxxsdsd”,
“timeStamp”:"…",

}

So I used deserialize JSON activity and extracted relevant field (“accessToken”). (See above discussion)

If your are having issues in using your access token follow this.

Actually I was not able to make a successful request by setting OAuth2Token parameter. But behind the scenes, HTTP headers are set as below for a OAuth2 authentication.

“Authorization”: “Bearer + accessToken (access token you get from server)”

So what i did was (as explained in above discussion), I manually set the header authorization field as above instead of using OAuth2 token.

Try this out.

1 Like

thanks for your response.

I am trying to get the access token by HTTP Request POST Method, after passing the required parameters in the body i am getting the response as “unsupported_grant_type”.

Can you please suggest.

It worked for me.

Thanks for your help.

Hi @ronak83garg

will you Please help in the same

Hi Chetan,

I have not passed anything in the OAuth2Token Properties of Http Request. Its showing as Nothing for me in Properties panel.

Get back in case of any further details required.

Thanks @ronak83garg