【发布时间】:2019-03-20 17:10:28
【问题描述】:
我想从外部访问 GCP 存储分区。所以我使用了谷歌提供的以下步骤。
- 创建了服务帐号
- 使用为服务帐户提供的私钥生成 jwt 令牌。
当我通过提供 jwt 令牌调用上述 API 来获取访问令牌时,它会出现以下错误。
{
"error": "invalid_scope",
"error_description": "Empty or missing scope not allowed."
}
提前致谢!
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
这是我用来生成 JWT 的 Java 代码
long now = System.currentTimeMillis();
try {
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("service.json"));
PrivateKey privateKey = credential.getServiceAccountPrivateKey();
String privateKeyId = credential.getServiceAccountPrivateKeyId();
Algorithm algorithm = Algorithm.RSA256(null, (RSAPrivateKey) privateKey);
String signedJwt = JWT.create()
.withKeyId(privateKeyId)
.withIssuer("***********@************-******.iam.gserviceaccount.com")
.withSubject("***********@************-******.iam.gserviceaccount.com")
.withAudience("https://www.googleapis.com/oauth2/v4/token")
.withIssuedAt(new Date(now))
.withExpiresAt(new Date(now + 3600 * 1000L))
.sign(algorithm);
System.out.println(signedJwt);
} catch(Exception e) {
System.out.println(e);
}
【问题讨论】:
-
显示您的代码,了解您如何创建 Signed-JWT 和您指定的范围。我写了一篇文章详细介绍了如何在 Python 中从服务帐户创建访问令牌:jhanley.com/…
-
@JohnHanley 这就是我生成 jwt 令牌的方式
-
编辑您的问题并格式化您的代码,以便我们阅读。
-
@JohnHanley 非常感谢您的帮助。我浏览了您的文章并找出了问题所在。
标签: google-cloud-platform google-cloud-storage