43
Views
3
Comments
Solved
Consume API with token and server action with cache
Application Type
Reactive

I consume a Microsoft API where for each request I need a token that expires in 1 hour. 

I need to perform a daily synchronization, and for each item in a list, I end up making 2 requests (one for the token and one for the request I want). I am currently making the token request in the OnBeforeRequest. 

If I encapsulate the token actions from OnBeforeRequest into a server action and add the cache option with a time of 1 hour, will it eliminate the need for 2 requests per item in the list? 

2019-01-07 16-04-16
Siya
 
MVP
Solution

I think your proposed strategy should work.  Ensure your caching strategy closely aligns with the token's expiration. You don't want to use an expired token for your requests, so timing is critical. Also Implement error handling for scenarios where the token may have expired or is invalid for some reason before the cache expires. This might involve checking the response from your first API request after obtaining the token from cache and refreshing the token if necessary. 

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

Hi Carlos,

You could also use a Session variable to store the token. Also make sure that you refresh the token often enough. There's no easy way to recover from an exception thrown because of the remote server issuing a 403 or the like if the token is expired.

UserImage.jpg
Carlos Rodrigues

Hi @Siya  and @Kilian Hekhuis
I used the cache approach. It's working for my object. Thank you for your help. 

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