go - 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 客户端获取身份验证令牌?这甚至可能吗?
感谢您的任何帮助!
解决方案
您只能将 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
推荐阅读
- javascript - onclick 事件未在 vue.js 中触发
- javascript - Nested HTTP requests in Firebase cloud function
- php - Laravel 表单复选框数据
- ios - 将 heic 转换为 JPG 并获取文件位置
- visual-studio - 运行 hololens 模拟器后 Windows 冻结
- java - 传递 Java ArryList
到 JNI C 函数并在 C 中打印列表 - c# - MainWindow上仅使用一个变量和一个事件绑定多个TextBox的文本属性
- python - Django 在哪里存储它的测试数据库
- json - 将可变长度 json 文件转换为 pandas 数据帧的更快实现
- authentication - 本机应用程序中第一方身份验证的最佳实践