547
Views
9
Comments
Consuming Authentication API with Bearer Token
Service Studio Version
11.53.41 (Build 62020)

I'm consuming Post Login API with Authentication Header which will provide Bearer Token in Response that I can use for our further Api. But can't figure out why authorization is not working correctly. 

I need to know how can I use Token which I get in response from Login API for other Apis Authentication dynamically. 


Screenshot (4).png
2022-02-25 19-42-59
Srinivas K Singadi

Hello Hammad Ali Jahangir

Store the Bearer Token in a variable or an entity record, so that it can be reused for future API requests.

Set the Authorization header of subsequent API requests to "Bearer" followed by the token value.

Make sure that the Authorization header is correctly formatted and included in the request headers

Create a global variable to store the Bearer Token:

Go to the Data tab in Service Studio.

Click on the New button to create a new global variable.

Name the variable "BearerToken" and set the data type to Text.

Save the variable.

In the action that consumes the Login API:

Parse the response JSON to extract the Bearer Token value.

Assign the Bearer Token value to the global variable "BearerToken" created in step 1.

In the action that consumes the other API:

Add a Preparation to the action.

Add a HTTP Header to the Preparation, set the Header Key to "Authorization", and set the Header Value to "Bearer " + BearerToken.

Make sure to replace "BearerToken" with the actual name of the global variable created in step 1.

Save the Preparation.

Call the API in the action flow and the Authorization header with the Bearer Token value should be included in the API request

2023-05-31 08-40-39
Hammad Ali Jahangir

I've used rest api of Post login it is giving token in response. How to store that response in entity or global variable ? 

Do I need to use onBeforeRequest or onAfterResponse event as well ? 

I'm confused

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

What is your confusion about? Srinivas explained in detail how you can do what you are asking in response to his post.

UserImage.jpg
Alexandre Yip

Hi Hammad, 

I assume that the retrieved token has a expiration in seconds involved. 

Create an entity TOKEN to save the token, fields Id as autonumber, token bearer as text, expiration as date time, it will work as a token cache. 

Calculate expiration datetime based on current date time+expiration in seconds. 

Whenever you do a call to other Apis check the entity Token, if is still valid retrieve the cached value, otherwise generate a new token and update the cache record. 

Hope that it helps you 


2023-05-31 08-40-39
Hammad Ali Jahangir

There is no time limit. The token changes once the Post Login Api runs. It has no time limit for expiration. Just want to know how to store that token which I'm getting from Post login Api response to use it dynamically when I'm using other Api.


UserImage.jpg
Alexandre Yip

Hi Hammad, 

You can use the same logic and save the the token in the entity without expiration field. 

For Api calls just get the token from the aggregate of TOKEN entity. 

Hope that it helps you 

2023-03-16 04-58-56
Ashish Lonkar

Hi Hammad,

After the assign in your screenshot you can use CreateOrUpdate Entity action and save the response in DB.

Is there any other issue you are facing ?

Thanks!

UserImage.jpg
Malavika Akhilesh

Hi, 

This is something like a  follow up question for the above. How will or should the above approach behave if the token value depends on each user. ie., the token in login response is for that particular user rather than any user who logs in.

UserImage.jpg
Alexandre Yip

Hi Malavika Akhilesh, 

Besides the token and expiry date time , you will need to add to the entity the user as well. 

In every request before requesting a new token check in the entity by user if there is a token and if the token is still valid. 

If yes retrieve the saved value. 

If not request a new one and save it by user. 

Hope that it helps you 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.