Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 如何通过python客户端库启用GCPAPI?_Google Cloud Platform_Google Api Python Client - Fatal编程技术网

Google cloud platform 如何通过python客户端库启用GCPAPI?

Google cloud platform 如何通过python客户端库启用GCPAPI?,google-cloud-platform,google-api-python-client,Google Cloud Platform,Google Api Python Client,我正在尝试创建一个python函数,该函数将支持GCP项目所需的服务API。我相信API调用将转到serviceusage API并使用services.enable或services.batchEnable方法。我不知道如何正确地构造查询 我已经能够使用GCP Python客户机库来自动化大多数项目创建函数,但不是这个。我直接使用了GCP文档中的代码,但对于这种特殊的方法几乎没有 from pprint import pprint from googleapiclient import dis

我正在尝试创建一个python函数,该函数将支持GCP项目所需的服务API。我相信API调用将转到serviceusage API并使用services.enable或services.batchEnable方法。我不知道如何正确地构造查询

我已经能够使用GCP Python客户机库来自动化大多数项目创建函数,但不是这个。我直接使用了GCP文档中的代码,但对于这种特殊的方法几乎没有

from pprint import pprint
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

project = 'projects/964030760997'  # TODO: Update placeholder value.

service = discovery.build('serviceusage', 'v1', credentials=credentials)
request = service.projects().services.list(project=project)
response = request.execute()


pprint(service)
这是我得到的答复:

Traceback (most recent call last):
  File "enable_apis.py", line 29, in <module>
    request = service.services().services.list(project=project)
AttributeError: 'Resource' object has no attribute 'services'

您引用了错误的文档。以下是服务使用API的链接:

这是我写的一个例子。笔记此代码不处理nextPageToken,因此只打印前50个服务。将代码添加到循环中

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

project = 'projects/myproject'

service = discovery.build('serviceusage', 'v1', credentials=credentials)
request = service.services().list(parent=project)

response = ''

try:
    response = request.execute()
except Exception as e:
    print(e)
    exit(1)

# FIX - This code does not process the nextPageToken
# next = response.get('nextPageToken')

services = response.get('services')

for index in range(len(services)):
    item = services[index]

    name = item['config']['name']
    state = item['state']

    print("%-50s %s" % (name, state))
此代码的输出类似于以下内容:

abusiveexperiencereport.googleapis.com             DISABLED
acceleratedmobilepageurl.googleapis.com            DISABLED
accessapproval.googleapis.com                      DISABLED
accesscontextmanager.googleapis.com                DISABLED
actions.googleapis.com                             DISABLED
adexchangebuyer-json.googleapis.com                DISABLED
adexchangebuyer.googleapis.com                     DISABLED
adexchangeseller.googleapis.com                    DISABLED
adexperiencereport.googleapis.com                  DISABLED
admin.googleapis.com                               ENABLED
adsense.googleapis.com                             DISABLED

非常感谢。我当然喜欢打印格式!