Google api Google API->无法从JWT获取访问令牌
我想使用从服务器生成的帐户服务令牌调用GoogleAPI分析 我按照导游的指示做了这件事 这是我生成签名JSON Web令牌的代码:Google api Google API->无法从JWT获取访问令牌,google-api,jwt,google-oauth,jwt-auth,Google Api,Jwt,Google Oauth,Jwt Auth,我想使用从服务器生成的帐户服务令牌调用GoogleAPI分析 我按照导游的指示做了这件事 这是我生成签名JSON Web令牌的代码: final GoogleCredential credential = GoogleCredential.fromStream(JWTSample.class.getResourceAsStream ("/account_secrets.json")) .createScoped(List.of(
final GoogleCredential credential = GoogleCredential.fromStream(JWTSample.class.getResourceAsStream ("/account_secrets.json"))
.createScoped(List.of(
"https://www.googleapis.com/auth/analytics",
"https://www.googleapis.com/auth/analytics.readonly")
);
final PrivateKey privateKey = credential.getServiceAccountPrivateKey();
final String privateKeyId = credential.getServiceAccountPrivateKeyId();
try {
long now = System.currentTimeMillis();
Algorithm algorithm = Algorithm.RSA256(null, (RSAPrivateKey) privateKey);
String signedJwt = JWT.create()
.withKeyId(privateKeyId)
.withIssuer("test-295@symbolic-folio-268713.iam.gserviceaccount.com")
.withAudience("https://oauth2.googleapis.com/token")
.withIssuedAt(new Date(now))
.withExpiresAt(new Date(now + 3600 * 1000L))
.sign(algorithm);
System.out.println(signedJwt);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
输出是一个有符号的JWT
当我调用Oauth2服务从签名的JWT生成访问令牌时
curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=<signed_JWT>' https://oauth2.googleapis.com/token
有什么提示吗?用echo${signedu JWT}|cut-d解码JWT断言声明集-f2 | base64-解码,您将看到没有scope属性 GoogleCredential实例具有作用域,但它们没有被传递给JWT构建器 在JWT.create之后使用此附加方法添加作用域:
使用echo${signedu JWT}cut-d解码JWT断言声明集-f2 | base64-解码,您将看到没有scope属性 GoogleCredential实例具有作用域,但它们没有被传递给JWT构建器 在JWT.create之后使用此附加方法添加作用域:
没有你的帮助,我永远也做不到这一点。非常感谢你,再次为谷歌文档感到羞耻。没有你的帮助,我从来没有做过这件事。非常感谢,再次为谷歌文档感到羞耻
{
"error": "invalid_scope",
"error_description": "Invalid oauth scope or ID token audience provided."
}
.withClaim("scope", credential.getServiceAccountScopesAsString())