【问题标题】:Log Google Cloud Service Account key created in Python API script记录在 Python API 脚本中创建的 Google Cloud Service 帐户密钥
【发布时间】:2018-12-20 21:00:19
【问题描述】:

我设法通过 Python API iamcredetials.googleapis.com 创建了一个服务帐户和一个自己的密钥,但我无法登录它,因为密钥是 P12 格式并以 dict 的形式接收,我可以'找不到登录密钥的方法。有没有办法创建 p12 文件或其他东西以便我可以使用密钥?

我尝试使用 oauth2clinet.service_account.ServiceAccountCredentials() 模块中提供的函数,但没有一个能成功加载它,我知道这个库中存在一定程度的弃用,也许我正在使用过时的方法。

当我使用_from_p12_keyfile_contents() 函数时,我离成功登录越近,它给出了“编码例程”错误,这超出了我的理解。

from oauth2client.service_account import ServiceAccountCredentials

from googleapiclient import discovery, errors, logging


default_creds = google_application_defaults()

service = discovery.build("iam", "v1", credentials=default_creds, cache_discovery = False)

key = service.projects().serviceAccounts().keys().create( name = serviceAccMail, body={}).execute()

creds = ServiceAccountCredentials._from_p12_keyfile_contents(accountEmail, newkey["privateKeyData"], "notasecret")

Error: [('asn1 encoding routines', 'asn1_check_tlen', 'wrong tag'), ('asn1 encoding routines', 'asn1_item_embed_d2i', 'nested asn1 error')]

登录此密钥的正确方法是什么?

【问题讨论】:

    标签: python google-cloud-platform google-oauth google-cloud-python google-iam


    【解决方案1】:

    不推荐使用 PFX (P12) 服务帐户格式。返回 Google 控制台并下载 Json 格式的服务帐户凭据。

    下载 Json 格式的凭据后,更改您的代码:

    from google.oauth2 import service_account
    
    sa_file = 'full/path/to/service_account.json'
    
    default_creds = service_account.Credentials.from_service_account_file(sa_file)
    

    [更新:以下代码将显示如何使用 P12 凭据]

    注意:P12 凭证不适用于所有 Google API(凭证类型不同)。此示例适用于 Google Discovery API。例如,此示例不适用于 google.cloud.storage

    '''
    Test program to use P12 credentials with Google Cloud Storage
    '''
    from oauth2client.service_account import ServiceAccountCredentials
    import googleapiclient.discovery
    
    # Details on the Google Service Account. The email must match the Google Console.
    project_id = 'development-123456'
    sa_filename = 'compute-engine.p12'
    sa_password = 'notasecret'
    sa_email = 'development-123456@developer.gserviceaccount.com'
    
    SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
    
    cred = ServiceAccountCredentials.from_p12_keyfile(
            sa_email,
            sa_filename,
            private_key_password=sa_password,
            scopes=SCOPES)
    
    client = googleapiclient.discovery.build('storage', 'v1', credentials=cred)
    
    buckets = client.buckets().list(project=project_id).execute()
    
    print('')
    print('Listing buckets:')
    for bucket in buckets['items']:
        print(bucket['name'])
    

    【讨论】:

    • 哦,那很糟糕。我希望我可以在自动化脚本中完成所有的密钥管理。绝对没有办法创建具有新格式的密钥吗?还是将 p12 密钥保存到字节生成的文件中,然后加载它?
    • 为什么这么糟糕?您只需以正确的格式签发凭证。您可以使用glcloud 或在控制台中创建凭据。大多数 Google SDK 不再支持 P12 凭据,并且用于加载 P12 凭据的 API 已被删除。
    • @IrribarraCristián - 我更新了我的示例以展示如何将 P12 凭据与 Google Discovery API 一起使用。这可能会对您有所帮助。
    • 感谢您的澄清。然而,在代码中创建密钥对我来说是必须的。如果我使用 os.system() 通过 gcloud 调用密钥创建,它会起作用吗?
    • 我的博客站点上有两篇文章可能会让您感兴趣。第一个展示了如何将 P12 文件转换为 Json 文件。第二个展示了如何从 json 凭据创建访问令牌。既不需要也不使用 Google SDK。 jhanley.com/…jhanley.com/…
    猜你喜欢
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2021-04-24
    • 2019-06-18
    • 2020-01-30
    • 2019-02-03
    • 2022-07-28
    相关资源
    最近更新 更多