Working with app-to-app Oauth access token refresh

Describe the issue/error/question

Hi, I am trying to configure an app-to-app connection, using Oauth. I was able to successfully do this with the HTTP request node. What I am struggling with is how to refresh the access token and use it in subsequent queries (used in Authorization header).

I could run the original HTTP node each time in my split in batch loop but that would be wasting quite a bit of resources and potentially be blocked by too many requests (rate limits).

What I have done so far is to use the webhook node in a second workflow to generate the access token and trigger the other workflow but this isn’t ideal as it would repeat the full loop instead of paginating/splitting the source list properly.

The access token is valid for 3599 seconds (1h minus 1 second) by design and I would like to refresh it so it’s always valid, with a 55 minute interval.

Another point for a possible issue is that my loop doesn’t process all the records in the spreadsheet and I am unsure why, it stops after a single record. I don’t know if it’s because I am using the merge node to combine the spreadsheet batch item (of 1) with the access token from the other node/workflow.

What would be great is to have the HTTP node to get the access token triggered by the Cron node at the interval While the other process/loop runs.

What is the error message (if any)?

Please share the workflow

(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)

Access token refresh workflow:

image

Child / parallel workflow consuming the access token:

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.165.0
  • Database you’re using (default: SQLite):
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: local install via NPM
1 Like

Hey @Pier-Andre_Maynard, n8n should handle OAuth2 credentials for you, including refreshing the access token (as long as a refresh token is issued). So typically you wouldn’t need to implement the logic manually.

Have you tried creating generic “OAuth2 API” credentials for your API and did you run into any trouble with this?

Hi @MutedJam, thanks for the follow-up. I have done a good amount of research and testing before posting. According to this feature request, this wasn’t supported by the Oauth2 credentials configuration.

Maybe this is supported now and was released at some point (I’d have to check the release notes for that to be sure).

One thing I do remember is that there was no way to configure the refresh token endpoint URL.

Hi @MutedJam I was searching again for a solution to my access token issue and eventually found this other question that was discussed. How to define custom environment variables available in the n8n workflow

Do you think I could store the access token in an environment variable in one workflow and use it (read it) in the other workflow running in parallel? If so, this would avoid requesting an access token for each item in the loop.

Workflow 1: Http request with a cron node to get and refresh the access token + store it as env variable.
Workflow 2: regular workflow consuming the access token for multiple operations & nodes.

Thank you

Maybe @jan can chime in. Would it be possible to set an environment variable via a workflow?

Here is my current code but I am getting an error with the function node.

Problem executing workflow

There was a problem executing the workflow:
"‘import’ and ‘export’ may only appear at the top level"

1 Like

Environment variable will not work but you could save it in a file on the hard drive or in a database Postgres, Redis, Mongo, …

Hi @jan & @MutedJam, thanks for looking at my thread earlier.

I am still hitting a snag with my workflow but found a way around the Oauth token.

Here is my current workflow and where it stops. Strangely it’s doing a single loop only in the Split in batch loop. Would you be able to figure out why that is the case?

Thank you!

Hi, just sharing my solution here for others that might be in a similar situation.

After a lot of research, I was able to figure out and test with the Merge node set to Wait mode before the HTTP request that generate the access token in my loop.

Here is the workflow.

I’ll mark this as solved with this as a solution, completing/fixing the loop.

Cheers!
Pier-André

1 Like