Hello there, I have a workflow where I have to iterate through a set of records and of each record send a POST request to an API. This API requires a bearer token in the as part of the request headers. Within the workflow, I first create a post request to the authentication endpoint to get the access token, and I pass the token as a variable into the POST requests for the records.
This works well enough, but I run into an issue when the token expires. When this happens I get a 401 error response from the API. This all makes sense.
Does anyone have an example workflow on how to take this scenario and return to the auth token part of the workflow? I am stumped as to how to loop back to retry the auth token sequence. Iāve attached a screenshot of the relevant sections.
Pragmatic approach: I wouldnāt probably bother with any sophisticated conditional handling, etc. Just re-create a new token before each request ā this operation is typically cheap and fast and if youāre not performing a five-digit number of requests it shouldnāt make any difference.
Eager approach: If your authentication API returns a token validity time stamp, store this, and check before each request if itās still valid ā if yes, continue. If not, branch out and request a new token, or refresh the current one.
Thanks for the suggestions. If anyone has a template to reacting to getting a 401 error and looping back to refresh the auth token at that point?
The reason Iām a little engaged with that method is 1) we have several processes where we are/would like to implement this and creating a token for each post would be sub-opmtimal and we also want to be able to react to different circumstances.
Hi @tsemana , I would go with the Pragmatic approach that @qqilihq suggested.
While ācreating a token for each post would be sub-opmtimalā, to a certain extent that is, creating a token before each request āis typically cheap and fastā. It outweighs the cost (effort) of implementing the approach that you want - I mean you canāt seem to be able to implement it.
If you really want to implement this, you can use the a condition loop (Generic Loop Start + Variable Condition Loop End), within which you can do a Try and Catch which will catch the error. If error, get new token and set your variable to loop again. If no error, continue and set variable to not loop. Youād have to set that up for every api call.
I appreciate the start. my description of āsub-optimalā was not in relation to cost or speed but in terms of usefulness to our usecase. And, to be fair, you canāt implement something until you learn how to do, which is what Iāve found this forum for. Not just to take the first suggestion.
The Bearer token expires after certain amount of time not requests right? Your loop tracks the current index so if you know one bearer token lasts for X calls you could regenerate the token based on current loop index. Certainly not perfect but just to keep it rather simple
br