【问题标题】:Enable google Cloud Resource Manager API by python client API通过 python 客户端 API 启用谷歌云资源管理器 API
【发布时间】:2021-11-02 05:41:28
【问题描述】:

如果没有启用api,通常会出现错误

HttpError: <HttpError 403 when requesting https://cloudresourcemanager.googleapis.com/v1/projects?alt=json returned "Cloud Resource Manager API has not been used in project 684373208471 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview?project=xxxxxxxxxx then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.". Details: "[{'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'Google developers console API activation', 'url': 'https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview?project=xxxxxxxxx'}]}, {'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'SERVICE_DISABLED', 'domain': 'googleapis.com', 'metadata': {'consumer': 'projects/xxxxxxxxxxx', 'service': 'cloudresourcemanager.googleapis.com'}}]">

google 是否提供 API 来启用某些 api?

【问题讨论】:

    标签: google-cloud-platform


    【解决方案1】:

    Google Cloud 提供了一个 API 来启用 API。这称为服务使用 API

    Service Usage REST API

    Service Usage Client Libraries

    我编写的用于显示 Python 中启用的 API 列表的示例代码:

    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
    

    API Documentation for my example code

    【讨论】:

    • 你只是显示代码来查看启用了哪些api,但是如何通过api启用它们
    • @LukAron - 您的问题是 Google 是否提供 API 来启用某些 api?。我回答了这个问题并提供了其他信息来帮助您使用 API。您没有指定语言。我认为 Python 将是一种易于阅读的语言,可以帮助您编写自己的软件。此链接显示要调用的 enable 方法:googleapis.github.io/google-api-python-client/docs/dyn/…
    • 如果能提供python示例将不胜感激,我也阅读过文档,但我觉得很难使用,在github.com/GoogleCloudPlatform/python-docs-samples中也找不到相关代码
    • @LukAron - 您的问题已得到解答。请不要使用 cmets 要求更多的东西。您可以创建另一个问题。
    猜你喜欢
    • 1970-01-01
    • 2020-09-07
    • 2018-09-15
    • 2017-11-23
    • 2018-02-21
    • 2020-05-16
    • 2017-10-28
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多