是否可以在不使用gcloud
的情况下获取Google Cloud服务帐户的授权持有人令牌?
也就是说,我想提出一个HTTP请求(可能是我的JSON密钥文件以某种方式签名),这将为我提供相当于
gcloud auth application-default print-access-token
此请求将在我自己的服务器上进行,我可能不希望安装gcloud
,并且我无权访问可能提供此内容的任何内部服务器元数据(例如,Compute的情况)发动机)。
是否有一些提供类似内容的oauth端点?
或者,是否有某种方法可以使用gcloud
生成长寿命令牌?
我是Google Cloud生态系统的新用户,请原谅我的无知和术语......
答案 0 :(得分:12)
我认为这正是您所寻找的:
https://developers.google.com/identity/protocols/OAuth2#serviceaccount
老实说,我不认为你想要达到的目标是正确的,正在运行
gcloud auth application-default print-access-token
你得到一个不打算做你想要的代币:
"当您开发通常使用服务帐户但需要在本地开发环境中运行代码并且更容易提供用户凭据时,此命令非常有用。"
这将指导您在此解决方案的实施中多一点: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
答案 1 :(得分:2)
对于你的第二个问题,“有没有办法用gcloud生成长寿命令牌?”,没有。但是,您可以使用刷新令牌生成新的访问令牌。 检查此问题的第一个答案:
OAuth2 and Google API: Access token expiration time?
此外,这是一个可以用作身份验证机制的python库示例:
答案 2 :(得分:1)
这就是golang的样子:
import(
"google.golang.org/api/compute/v1"
"google.golang.org/api/container/v1"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/api/transport"
)
ctx := context.Background()
creds, err := transport.Creds(ctx, option.WithScopes(compute.CloudPlatformScope))
token, err := creds.TokenSource.Token()
答案 3 :(得分:0)
不需要库或api。您可以使用
获取它jquery.contextMenu.min.js
如果您需要在Shell脚本中使用它,则很有用。
答案 4 :(得分:0)
我认为这就是您所需要的:
https://github.com/googleapis/google-auth-library-java#explicit-credential-loading
<块引用>显式凭据加载 要从服务帐户 JSON 密钥获取凭据,请使用 GoogleCredentials.fromStream(InputStream) 或 GoogleCredentials.fromStream(InputStream, HttpTransportFactory)。请注意,必须在访问令牌可用之前刷新凭据。
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/path/to/credentials.json"));
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
// OR
AccessToken token = credentials.refreshAccessToken();