How to Authorize Gmail API Using Service Account Credentials for Multiple Accounts in n8n?

Hello n8n Community!

I’m working on a workflow that:

  1. Watches for new emails in Gmail.
  2. Uses AI to analyze the email content.
  3. Labels the email based on the analysis.

The challenge is that I have 6 Gmail accounts, and duplicating the workflow for each account is becoming unmanageable. I’ve been advised to use the n8n-nodes-run-node-with-credentials-x community node to handle credentials dynamically.

Here’s what I’ve done so far:

  • I plan to store Service Account credentials (JSON file containing client_email and private_key) for all 6 accounts in the community node.
  • Since there are multiple accounts, I can’t rely on pre-defined credentials. Instead, I need to dynamically authorize each account within the workflow.

The problem I’m facing:

  1. To authorize the Gmail API, I need an access token. However, the Service Account JSON file doesn’t directly provide this.
    How do I generate an access token using the Service Account credentials?
  2. I know that the access token expires every 1 hour.
    How do I manage the token renewal dynamically for multiple accounts within my workflow?

Can anyone please help me setup just the https request authorization part?

This is how the workflow looks like:

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @Rohit_Gurav

We automatically handle the token refresh internally so you don’t need to worry about that, we also don’t you access to the tokens as it isn’t needed.

If you are planning to manually store and handle the authentication you will need to use some http request nodes to manage the authentication handshake which will likely get more complicated than having multiple workflows.

What you could do instead is use an execute workflow node that either calls a workflow that uses the correct credentials or one workflow with a switch node to call the correct node.

@Jon so you want me to use Gmail trigger node to fetch the unread emails and than call a centralized workflow to handle the Action part?

Question is

  1. you don’t want me to use the access token stuff
  2. you want me to approach this problem with native nodes and not HTTPS node is it?

why not create a workflow whit n8n create credential and create new credential from your json?

and after why not create workflow with n8n create or update workflow? and set the node withnew credentials?

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