dongtuhe0506 2019-08-16 20:40
浏览 121
已采纳

如何使用服务帐户密钥JSON与服务帐户JSON发布到pubsub

Is this possible?

When I attempt to use the following Service Account Key JSON:

{
 "keyAlgorithm": "KEY_ALG_RSA_2048",
 "keyOrigin": "GOOGLE_PROVIDED",
 "name": "projects/XXXX",
 "privateKeyData": "XXXXXX",
 "privateKeyType": "TYPE_GOOGLE_CREDENTIALS_FILE",
 "validAfterTime": "2019-08-16T19:45:25Z",
 "validBeforeTime": "2029-08-13T19:45:25Z"
}

I get the following error:

client: pubsub: google: error getting credentials using GOOGLE_APPLICATION_CREDENTIALS environment variable: missing 'type' field in credentials

Am I missing something? It apparently only wants to work with Service Account JSON. I'd rather not hand out those credentials.

Any advice?

  • 写回答

1条回答 默认 最新

  • dsc56927 2019-08-18 04:19
    关注

    The reason for the error is that you are using the raw output from the API service.projects.serviceAccounts.keys.create.

    Look at the output in your question. The key privateKeyData contains the base64 encoded value of the service account JSON key file. Base64 decode that value and use as JSON for input when creating credentials.

    Python Example:

    from google.oauth2 import service_account
    info = json.loads(base64.b64decode(privateKeyData))
    credentials = service_account.Credentials.from_service_account_info(info)
    

    Go Example:

    import b64 "encoding/base64"
    import "golang.org/x/oauth2/google"
    scope := "https://www.googleapis.com/auth/cloud-platform"
    s, _ := b64.StdEncoding.DecodeString(privateKeyData)
    ctx := context.Background()
    creds, err := google.CredentialsFromJSON(ctx, s, scope)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置