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

时间:2018-09-08 21:22:07

标签: go google-cloud-platform google-authentication automl google-cloud-automl

由于Google AutoML没有golang客户端,因此我必须使用AutoML http客户端。为此,需要来自Google的身份验证令牌,该令牌来自运行以下cli命令:

gcloud auth application-default print-access-token

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

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

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

谢谢您的帮助!

1 个答案:

答案 0 :(得分: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