I am trying to call external REST API from Salesforce. Sometimes I get error 401 with messages token expired or token invalid. Is there a way to prevent calling API when token is not valid. The thing is that I have only token as String, I do not have private/public key so I cannot use Crypto.verify() method. I use JWT token for authentication. This issue is also happening in Postman. But, when my token is valid I got 200 code, status OK. Any help?
Asked
Active
Viewed 770 times
0
-
3Duplicate of https://salesforce.stackexchange.com/questions/376124/validate-string-jwt-using-apex – identigral May 17 '22 at 16:04
1 Answers
1
You cannot do something without having necessary information which requires it to do what you are trying to do. tldr; You cannot decode JWT token without its keys (unless JTW is just signed and not encrypted).
Your best bet (which is safe one) is to catch Invalid Auth error, re-init the session and then call again. If you include this check in lower-layer of your application service stack, one time implementation will handle for many api calls.
metasync
- 1,772
- 1
- 8
- 20