Need to refresh authentication token, any example?

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.

Thanks in advance for your help!

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.

4 Likes

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.

Again, I appreciate the 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

Thanks for the suggestion @Daniel_Weikert

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.