GCP Python SDK - 在没有 gcloud 的情况下启用 API

GCP Python SDK - Enable APIs without gcloud

我已使用此处的文档 https://cloud.google.com/endpoints/docs/frameworks/enable-api#gcloud 通过 gcloud CLI 工具启用新的 API。我有一个对我的项目具有所有者角色的服务帐户,是否可以从 python SDK 中启用 API 而无需使用 gcloud?或者,是否可以从 python SDK 调用 gcloud CLI 工具?

您可以使用 discovery API client library for Service Usage API

您还可以对 Service Usage API enable service endpoint

执行经过身份验证的调用

如果您有问题并且想要代码示例,请告诉我。

根据官方文档:

Enabling APIs

You can enable Cloud APIs for a project using the console

You can also enable and disable Cloud APIs using Cloud SDK and Service Usage API

因此我建议 make a HTTP request 使用 python 到此 url :POST https://serviceusage.googleapis.com/v1/{name=*/*/services/*}:enable

这是我用来启用analytics.googleapis.comAPI

curl命令
OAUTH_TOKEN=`gcloud auth print-access-token`

curl -X POST -H "Authorization: Bearer $OAUTH_TOKEN" -H "Content-Type: application/json" https://serviceusage.googleapis.com/v1/projects/your-project/services/analytics.googleapis.com:enable


Response body
.......
"state": "ENABLED",
      "parent": "projects/xxxxxxx"

.