【问题标题】:Accessing GAE log files using Google Cloud Logging (Python)使用 Google Cloud Logging (Python) 访问 GAE 日志文件
【发布时间】:2017-08-17 17:03:38
【问题描述】:

我们有一个正在运行的 Google App Engine (GAE) 服务,我们希望将其日志下载到我们的服务器上存档。

GAE 有一个服务帐户,其凭据已作为 JSON 文件下载到我们的服务器。以下代码在我们的服务器上运行,尝试为日志服务创建客户端:

from google.cloud import logging
client = logging.Client.from_service_account_json('credentials.json')

结果:

ValueError: Service account info was not in the expected format, missing fields token_uri, client_email.

错误消息很清楚,但不清楚的是为什么在为此目的创建的 JSON 文件中需要这些字段?我们是否使用了错误类型的服务帐户的凭据?

【问题讨论】:

    标签: google-app-engine google-cloud-platform


    【解决方案1】:

    您需要获取包含私钥凭据的服务帐户文件,它基本上与您拥有的文件不同。 您可以获取它,或者通过转到https://console.developers.google.com/iam-admin/iam/ 来获取一个新的,然后选择您的项目,然后选择“服务帐户”并创建一个新的作为该项目的角色“查看者”(或使用一个已经存在并且点击“创建新密钥”)

    “密钥”是一个 json 或 p12 文件,将在您创建帐户(或在其中使用“创建新密钥”)时下载,其中包含适用于您的代码的正确字段和凭据。

    下载的“key”文件结构示例(选择 JSON 时):

    {
      "type": "service_account",
      "project_id": "zeta-handler-9999",
      "private_key_id": "123456789deedbeaf",
      "private_key": "-----BEGIN PRIVATE KEY-----\nREDACTED REDACTED...-----END PRIVATE KEY-----\n",
      "client_email": "projectname-service-account@zeta-handler-9999.iam.gserviceaccount.com",
      "client_id": "12345678909999",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/projectname-service-account%40zeta-handler-9999.iam.gserviceaccount.com"
    }
    

    使用该“密钥”文件的示例代码 (python):

    #!/usr/bin/env python
    import google.auth
    from google.oauth2 import service_account
    
    credentials = service_account.Credentials.from_service_account_file('downloaded_key.json')
    scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/drive.metadata.readonly'])
    

    【讨论】:

    • 新角色必须是viewer,不是ownereditor吗?
    • 它可以是任何你想要的,这只是一个例子。 viewer 是最安全的默认值。
    【解决方案2】:
    {
      "type": "service_account",
      "project_id": "",
      "private_key_id": "",
      "private_key": "-----BEGIN PRIVATE KEY-----
    something long here
    ---END PRIVATE KEY-----\n",
      "client_email": "",
      "client_id": "",
      "auth_uri": "",
      "token_uri": "",
      "auth_provider_x509_cert_url": "",
      "client_x509_cert_url": ""
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2021-08-06
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多