首页 > 解决方案 > Golang 和 gcloud API:如何获取身份验证令牌

问题描述

因为 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 客户端获取身份验证令牌?这甚至可能吗?

感谢您的任何帮助!

标签: gogoogle-cloud-platformgoogle-authenticationautomlgoogle-cloud-automl

解决方案


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

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


推荐阅读