gpt4 book ai didi

Golang 和 gcloud API : how to get an auth token

转载 作者:IT王子 更新时间:2023-10-29 02:36:52 26 4
gpt4 key购买 nike

因为 Google AutoML 没有 golang 客户端,所以我不得不使用 AutoML http 客户端。为此,需要来自谷歌的身份验证 token ,该 token 来自运行以下 cli 命令:

gcloud auth application-default print-access-token

我目前正在使用一个也可以访问 AutoML 的凭据 json 文件来验证我的 Golang 服务器(示例用法)

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

我的问题是:如果我有一个 JSON 凭据文件,我将如何从 Golang Google 客户端获取授权 token ?这可能吗?

感谢您的帮助!

最佳答案

您只能将 API token 用于某些 Google Cloud API。 Google Cloud 不鼓励使用 token ,您可以在这篇文章中阅读:

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

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


storageClient,错误:= storage.NewClient(ctx)

还建议使用“ServiceAccount”,这样您的应用程序使用的凭据就可以成为它的范围。您可以在这里阅读更多内容:

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

关于Golang 和 gcloud API : how to get an auth token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52239503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com