请注意,上述示例将替换所有现有的服务级别 IAM 政策。如需增量授予新角色,您需要在请求正文中添加所有 IAM 政策。
在服务使用方级层授予角色
您可以在服务使用方级别授予角色 roles/servicemanagement.serviceController。服务使用方级别权限要求对 Service Control API 的请求必须至少包含一个有效的服务使用方项目 ID。以下示例使用了使用入门指南的使用 curl 进行测试部分中定义的 gcurl 别名:
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Service Control API Access Control\n\nTo invoke the Service Control API for a\n[managed service](/service-infrastructure/docs/glossary#managed), the caller must have the following\n[Identity and Access Management (IAM)](/iam) permissions on the service:\n\n- [`services.check`](/service-infrastructure/docs/service-control/reference/rest/v1/services/check) requires permission `servicemanagement.services.check`.\n- [`services.report`](/service-infrastructure/docs/service-control/reference/rest/v1/services/report) requires permission `servicemanagement.services.report`.\n- [`services.allocateQuota`](/service-infrastructure/docs/service-control/reference/rest/v1/services/allocateQuota) requires permission `servicemanagement.services.quota`.\n\nThe IAM roles `roles/servicemanagement.serviceController`,\n`roles/owner` and `roles/editor` include these permissions and can each be used\nto grant them. We recommend using the IAM role\n`roles/servicemanagement.serviceController` to run your managed services. While\n`roles/owner` and `roles/editor` also grant these permissions, the narrower role\nis better for security reasons.\n\nResource model\n--------------\n\nIAM applies IAM policies on resources to control\nwho can perform what actions on the resources. The Service Control API\nuses the following resources to provide its functionality:\n\n- **Service producer project** : A [service producer](/service-infrastructure/docs/glossary#producer) project may own zero or more managed services. The service producer project is the parent of services in this hierarchy.\n- **Managed service** : A managed service may have zero or more [service consumers](/service-infrastructure/docs/glossary#consumer).\n- **Service consumer** : A [service consumer](/service-infrastructure/docs/glossary#consumer) refers to a Google Cloud project that has enabled the service.\n\nThe IAM access control is applied to the resource model. If a\nrole is granted at the service producer project level, it affects all managed\nservices owned by the producer project. If a role is granted at the managed\nservice level, it affects all service consumers of the managed service.\n\nIt is highly recommended that you only create one managed service per service\nproducer project for security and isolation reasons. Otherwise, for example, if\nthe service producer project runs out of quota for sending requests to the\nService Control API, multiple managed services will be impacted.\n\nIf you have a multi-tenant service, you should grant the role\n`roles/servicemanagement.serviceController` at the managed service\nlevel. If you have a single-tenant service, in other words, a service where\neach service consumer gets its own instance of your managed service, you should\ngrant the role at the service consumer level. For background data processing\nthat affects all service consumers, you should grant the role at the managed\nservice level.\n\nGrant roles\n-----------\n\nTo call the Service Control API, you must grant the necessary roles to\nthe callers. You can grant the roles via one of the following three approaches.\nYou need to be an owner of the service producer project so that you can grant\nthe necessary roles.\n\n### Grant a role at the service producer project level\n\nYou can grant the necessary roles on the project that a managed service belongs\nto, following the instructions in\n[Granting, changing, and revoking access to resources](/iam/docs/granting-changing-revoking-access), or use the\nGoogle Cloud CLI\n[add-iam-policy-binding](/sdk/gcloud/reference/projects/add-iam-policy-binding)\ncommand to grant the roles.\n\nFor example, you can grant the roles to a service account, such as\n`foo@developer.gserviceaccount.com`: \n\n```\ngcloud projects add-iam-policy-binding PRODUCER_PROJECT_ID --member serviceAccount:SERVICE_ACCOUNT --role roles/servicemanagement.serviceController\n```\n\nSimilarly, you can grant the roles to a user account, such as `bar@gmail.com`: \n\n```\ngcloud projects add-iam-policy-binding PRODUCER_PROJECT_ID --member user:USER_ACCOUNT --role roles/servicemanagement.serviceController\n```\n\n### Grant a role at the service level\n\nYou can grant the role `roles/servicemanagement.serviceController` at the\nservice level using `curl`. The following example uses the `gcurl` alias\ndefined in the\n[Test with curl](/service-usage/docs/set-up-development-environment#test)\nsection of the **Getting started** guide: \n\n```\ngcurl -d \"{\n 'policy': {\n 'bindings': [ {\n role: 'roles/servicemanagement.serviceController',\n members: 'serviceAccount:SERVICE_ACCOUNT'\n } ]\n }\n}\" https://servicemanagement.googleapis.com/v1/services/YOUR_SERVICE_NAME:setIamPolicy\n```\n\nPlease note the above example will replace all existing service level IAM policies. To grant the new role incrementally, you need to include all IAM policies in the request body.\n\n### Grant a role at the service consumer level\n\nYou can grant the role `roles/servicemanagement.serviceController` at the\nservice consumer level. Service consumer level permissions require that the\nrequest to the Service Control API must contain at least one valid\nservice consumer project ID. The following example uses the `gcurl` alias\ndefined in the\n[Test with curl](/service-usage/docs/set-up-development-environment#test)\nsection of the **Getting started** guide: \n\n```\ngcurl -d \"{\n 'policy': {\n 'bindings': [ {\n role: 'roles/servicemanagement.serviceController',\n members: 'serviceAccount:SERVICE_ACCOUNT'\n } ]\n }\n}\" https://servicemanagement.googleapis.com/v1/services/YOUR_SERVICE_NAME/consumers/CONSUMER_PROJECT_NUMBER:setIamPolicy\n```\n\nPlease note the above example will replace all existing consumer project level IAM policies. To grant the new role incrementally, you need to include all IAM policies in the request body.\n\nFor more information, see [Cloud Auth Guide](/docs/authentication)."]]