Elv*_*man 5 python authentication google-cloud-platform google-cloud-iam google-auth-library
我想为客户端创建 GCP 的自动部署。
为了做到这一点,我打开了一个页面让他们用谷歌登录,然后启用 IAM API 和服务使用 API。
然后我创建了一个服务帐户,我想从现在开始使用它,以便按需启用其他必需的 API,而不是一次性启用。
当我尝试启用 cloudkms API 时,我得到
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://serviceusage.googleapis.com/v1/projects/x-y-z/services/cloudkms.googleapis.com?alt=json returned "The caller does not have permission"
Run Code Online (Sandbox Code Playgroud)
我尝试使用从创建服务帐户的响应中创建的服务帐户凭据 (google.auth.jwt.Credentials),并添加了所有必需的权限。我不想将角色所有者授予服务帐户,因为我希望该帐户具有尽可能少的权限。
当我尝试使用用户的权限获取 cloudkms API 的状态时,它起作用了。
我已经看到一些解决方案解决了我需要在此处为服务帐户创建凭据的问题:https : //console.developers.google.com/apis/credentials但我确实也需要以编程方式执行此操作。
我的代码:
credentials = jwt.Credentials.from_service_account_file(service_account_info['email'] + '.json', audience="https://www.googleapis.com/auth/cloud-platform")
# credentials = GoogleCredentials.get_application_default() - it works with this
service_usage = googleapiclient.discovery.build('serviceusage', 'v1', credentials=credentials)
service_usage.services().get(name="projects/<project_id>/services/cloudkms.googleapis.com").execute()
Run Code Online (Sandbox Code Playgroud)
上面提到了错误。
您需要 Cloud IAM 权限serviceusage.services.enable才能启用服务。根据您需要的功能(例如列表服务),您需要serviceusage.services.list.
通常,您添加roles/serviceusage.serviceUsageAdmin包含以下权限的角色:
| 归档时间: |
|
| 查看次数: |
4070 次 |
| 最近记录: |