Google Cloud denies iam.serviceAccounts.getAccessToken when impersonating a service account
Permission 'iam.serviceAccounts.getAccessToken' denied on resourceShort 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.
Fix it fast
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.com2Match what you see, then jump to its fix
- 1
The 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
The policy lists roles/iam.serviceAccountUser for the caller but not the Token Creator roleService Account User was granted instead of Token CreatorFix
- 1
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.com2Fix
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.serviceAccountTokenCreator3Verify
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.comConfirmed by docs.cloud.google.com
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.com2Fix
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.serviceAccountTokenCreator3Verify
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.comConfirmed by docs.cloud.google.com
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
Google Cloud client library cannot find Application Default Credentials in Node.js
Could not load the default credentials
Google client libraries look for Application Default Credentials in a fixed order: the GOOGLE_APPLICATION_CREDENTIALS variable, the file made by gcloud auth application-default login, then the service account attached to the machine.
3 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix