Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Google Cloud denies iam.serviceAccounts.getAccessToken when impersonating a service account

ErrorPermission 'iam.serviceAccounts.getAccessToken' denied on resource

Short answer

This error appears when you or your app impersonate a service account without the right to create tokens for it. The permission iam.serviceAccounts.getAccessToken comes with the Service Account Token Creator role, granted on that service account. The Service Account User role does not include it.

  • Applies to Google Cloud CLI, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the service account email in the error, then list who can use it and with which role.

    gcloud iam service-accounts get-iam-policy SA_NAME@PROJECT_ID.iam.gserviceaccount.com
  2. 2Match what you see, then jump to its fix

    1. 1The policy from the first step has no roles/iam.serviceAccountTokenCreator entry for your user or for the identity your app runs asThe caller has no Token Creator role on the service accountFix
    2. 2The policy lists roles/iam.serviceAccountUser for the caller but not the Token Creator roleService Account User was granted instead of Token CreatorFix

What does the error look like?

  • Permission 'iam.serviceAccounts.getAccessToken' denied on resource

Cause 1 of 2

The caller has no Token Creator role on the service account

1Check

Impersonation, including gcloud --impersonate-service-account and application default credentials created with it, calls for short-lived credentials. That needs iam.serviceAccounts.getAccessToken, which the Token Creator role includes.

gcloud iam service-accounts get-iam-policy SA_NAME@PROJECT_ID.iam.gserviceaccount.com

2Fix

Grant the role on the service account itself, to the identity that is doing the impersonating.

gcloud iam service-accounts add-iam-policy-binding SA_NAME@PROJECT_ID.iam.gserviceaccount.com \
  --member=user:my-user@example.com \
  --role=roles/iam.serviceAccountTokenCreator

3Verify

Wait a couple of minutes (policy changes can take up to 7 minutes or more), then repeat the impersonated command.

gcloud auth application-default login --impersonate-service-account SA_NAME@PROJECT_ID.iam.gserviceaccount.com

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 2 of 2

Service Account User was granted instead of Token Creator

1Check

Service Account User lets a principal attach the service account to resources. It does not allow creating tokens for it.

gcloud iam service-accounts get-iam-policy SA_NAME@PROJECT_ID.iam.gserviceaccount.com

2Fix

Keep the User role if you also attach the account to resources, and add Token Creator for impersonation.

gcloud iam service-accounts add-iam-policy-binding SA_NAME@PROJECT_ID.iam.gserviceaccount.com \
  --member=user:my-user@example.com \
  --role=roles/iam.serviceAccountTokenCreator

3Verify

Run the command that failed again once the change has propagated.

gcloud auth application-default login --impersonate-service-account SA_NAME@PROJECT_ID.iam.gserviceaccount.com

Confirmed by docs.cloud.google.com

Did this fix it?

How do I stop it coming back?

  • Grant Token Creator on the single service account, not on the whole project.
  • Use impersonation with short-lived tokens instead of downloading service account key files.
  • Add the role to a group, not to individual users, so new team members get it once.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.