Replace & Save Dynamic Token using HTTP node

Describe the issue/error/question

Hi, i’m looking to connect to mogenius API ( Refresh-Tokens (mogenius.com)) using the http node to manage my container hosted on their platform.

Login API

  1. To use the API, i will need to send a login request to generate an accessToken.
  2. The accesstoken is required for all other API functions.
  3. The accesstoken will expire after like 3 days.
  4. The accesstoken is issued per device.
  5. They will issue an accessToken along with a refreshToken. refreshToken is used to generate new accessToken (with new validity) for the same device.

The issue,
I can simply use the Login API request using http node to generate a new accesstoken and continue doing other stuffs using that token on single execution. If i’m using this method, each execution will register new device with their own accesstoken, and I will ended up with a super long list of devices/tokens in my account dashboard. They suggested to utilise the refreshToken API to generate a new accessToken for the same device. So there will be only 1 device linked to the account with dynamic accessToken every time I called the refresh token request before doing other tasks.

Need help on…

  1. The accessToken cannot be saved as permanent credential as it has short validity.
  2. I’m looking for a way to use the refresh token API to generate a new accessToken to be passed on the next node, at the same time save the new refreshToken somewhere in n8n to be called/used for the next execution cycle. Each cycle will replace & save the new refreshToken string.

Please share the workflow

I’m executing this Login API manually only 1 time time to register the n8n connection and generate the initial refreshToken. Tried to save it as binary as the source of refreshToken for future execution.

Information on your n8n setup

  • n8n version: 0.217.2
  • Database you’re using (default: SQLite): postgresql
  • Running n8n with the execution process [own(default), main]: default
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: docker

Hi @lamanrasmi, n8n doesn’t really have a mechanism to auto-refresh header authentication values I am afraid. Your current approach seems like the easiest to read-one (from a workflow perspective) for me.

  1. The accessToken cannot be saved as permanent credential as it has short validity.
  2. I’m looking for a way to use the refresh token API to generate a new accessToken to be passed on the next node, at the same time save the new refreshToken somewhere in n8n to be called/used for the next execution cycle. Each cycle will replace & save the new refreshToken string.

If you want to re-use your token a little longer you would need to store somewhere and access it whenever needed.

Tried to save it as binary as the source of refreshToken for future execution

This sounds to me like it could do the job (have one workflow run every 3 days to refresh your token, have another workflow simply reading the current token from wherever you store it). Did you encounter any issues with this approach?

On a separate note, it seems your original post included base64-encoded credentials. I’ve removed them, but you probably want to invalidate them in your system as well.

Thanks a lot for your hints. I managed to get it to work using these flow. The top flow I can run manually once a year just in case i need to reset the device token.
The daily scheduled flow is working now. Every time it executes, it replaces the old refreshtoken with a new one to be used the next day.

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