Sets the IAM policy that is attached to a ServiceAccount.
Use this method to grant or revoke access to the service account. For example, you could grant a principal the ability to impersonate the service account.
This method does not enable the service account to access other resources. To grant roles to a service account on a resource, follow these steps:
Call the resource's getIamPolicy method to get its current IAM policy.
Edit the policy so that it binds the service account to an IAM role for the resource.
Call the resource's setIamPolicy method to update its IAM policy.
REQUIRED: The complete policy to be applied to the resource. The size of the policy is limited to a few 10s of KB. An empty policy is a valid policy but certain Google Cloud services (such as Projects) might reject them.
OPTIONAL: A FieldMask specifying which fields of the policy to modify. Only the fields in the mask will be modified. If no mask is provided, the following default mask is used:
paths: "bindings, etag"
This is a comma-separated list of fully qualified names of fields. Example: "user.displayName,photo".
Response body
If successful, the response body contains an instance of Policy.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-05-21 UTC."],[[["\u003cp\u003eThis method sets the IAM policy for a specified ServiceAccount, granting or revoking access, such as the ability for a principal to impersonate the service account.\u003c/p\u003e\n"],["\u003cp\u003eThe HTTP request uses a \u003ccode\u003ePOST\u003c/code\u003e method and includes a resource identifier in the URL, following gRPC Transcoding syntax, and requires a resource string in path parameter to identify the resource.\u003c/p\u003e\n"],["\u003cp\u003eThe request body is required and uses a JSON representation, specifying the policy and an optional update mask, with the policy having a size limit.\u003c/p\u003e\n"],["\u003cp\u003eA successful request returns a Policy object as the response body.\u003c/p\u003e\n"],["\u003cp\u003eAuthorization requires either the \u003ccode\u003ehttps://www.googleapis.com/auth/iam\u003c/code\u003e or \u003ccode\u003ehttps://www.googleapis.com/auth/cloud-platform\u003c/code\u003e OAuth scope.\u003c/p\u003e\n"]]],[],null,["# Method: projects.serviceAccounts.setIamPolicy\n\n- [HTTP request](#body.HTTP_TEMPLATE)\n- [Path parameters](#body.PATH_PARAMETERS)\n- [Request body](#body.request_body)\n - [JSON representation](#body.request_body.SCHEMA_REPRESENTATION)\n- [Response body](#body.response_body)\n- [Authorization scopes](#body.aspect)\n- [Examples](#examples)\n- [Try it!](#try-it)\n\nSets the IAM policy that is attached to a [ServiceAccount](/iam/docs/reference/rest/v1/projects.serviceAccounts#ServiceAccount).\n\nUse this method to grant or revoke access to the service account. For example, you could grant a principal the ability to impersonate the service account.\n\nThis method does not enable the service account to access other resources. To grant roles to a service account on a resource, follow these steps:\n\n1. Call the resource's `getIamPolicy` method to get its current IAM policy.\n2. Edit the policy so that it binds the service account to an IAM role for the resource.\n3. Call the resource's `setIamPolicy` method to update its IAM policy.\n\nFor detailed instructions, see [Manage access to project, folders, and organizations](https://cloud.google.com/iam/help/service-accounts/granting-access-to-service-accounts) or [Manage access to other resources](https://cloud.google.com/iam/help/access/manage-other-resources).\n\n### HTTP request\n\n`POST https://iam.googleapis.com/v1/{resource=projects/*/serviceAccounts/*}:setIamPolicy`\n\nThe URL uses [gRPC Transcoding](https://google.aip.dev/127) syntax.\n\n### Path parameters\n\n### Request body\n\nThe request body contains data with the following structure:\n\n### Response body\n\nIf successful, the response body contains an instance of [Policy](/iam/docs/reference/rest/v1/Policy).\n\n### Authorization scopes\n\nRequires one of the following OAuth scopes:\n\n- `https://www.googleapis.com/auth/iam`\n- `\n https://www.googleapis.com/auth/cloud-platform`\n\nFor more information, see the [Authentication Overview](/docs/authentication#authorization-gcp)."]]