Access token in backend

evazquezl
Participant I

When use an endpoint in backend this ask me a token, this token can generate in other request for backend and expiry after 1 hour, so its necesary generate a new token.

Exist in Apigee any mechanism for manage tokens in backend?

or

What is the best option for detect error 401 and request other token?

Than you.

Solved Solved
0 3 242
1 ACCEPTED SOLUTION

robert
Participant V

Don't wait for a 401. Instead, call to the external token endpoint and cache the external token using the populate cache. Make sure to set the cache expiration to expire in 58 minutes so that it expires prior to the token expiring. Then, upon cache expire, you call to get another token and cache it again. For any call that needs the token, grab it from the cache using the lookup cache policy

Also, if you get a 401 from backend, just return the 401 back to the client and expire the cache. Next time client calls the token will be renewed and populated into the cache according to above.

Hopefully this helps? If so, please click accept.

View solution in original post

3 REPLIES 3

robert
Participant V

Don't wait for a 401. Instead, call to the external token endpoint and cache the external token using the populate cache. Make sure to set the cache expiration to expire in 58 minutes so that it expires prior to the token expiring. Then, upon cache expire, you call to get another token and cache it again. For any call that needs the token, grab it from the cache using the lookup cache policy

Also, if you get a 401 from backend, just return the 401 back to the client and expire the cache. Next time client calls the token will be renewed and populated into the cache according to above.

Hopefully this helps? If so, please click accept.

Thank you Robert.

One question, when you say "Then, upon cache expire, you call to get another token and cache it again."

How I can detect cache expire, for to get another token? Or first I need to do a Lookup Cache and if this don't return a value in flow variable, it means, I must get another token?

Regards.

YES - that's what it means. Do the LookupCache, check the returned value, and get a new token if the old one is gone.