Golang和gcloud API:如何获取身份验证令牌

问题描述 投票:0回答:1

由于Google AutoML没有golang客户端,因此我必须使用AutoML http客户端。要这样做,需要来自google的auth令牌,该令牌来自运行以下cli命令:

gcloud auth application-default print-access-token

我目前正在使用可以访问AutoML的凭据json文件来为我的Golang服务器进行身份验证(示例用法)

storageClient, err := storage.NewClient(ctx, option.WithCredentialsFile(gcloudCredsJSONPath))

我的问题是:如果我有JSON凭证文件,如何从Golang Google客户端获取身份验证令牌?这甚至可能吗?

感谢您的任何帮助!

go google-cloud-platform google-authentication automl google-cloud-automl
1个回答
2
投票

您只能将API令牌与某些Google Cloud API一起使用。 Google Cloud不鼓励使用令牌,因为您可以在本文中阅读:

https://cloud.google.com/docs/authentication/

如果您的生产环境也是Google Cloud,则可能根本不需要使用任何JSON文件。 Google Cloud具有“DefaultCredentials”概念,它通过环境注入您的服务。您可以将代码简化为:

storageClient, err := storage.NewClient(ctx)

还建议使用“ServiceAccount”,以便应用程序使用的凭据可以作为其范围。你可以在这里阅读更多:

https://cloud.google.com/docs/authentication/getting-started

© www.soinside.com 2019 - 2024. All rights reserved.