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

如何使用服务帐户密钥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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示