Google API->无法从JWT获取访问令牌

问题描述 投票:1回答:1

我想使用服务器生成的帐户服务令牌来调用Google API Analytics。

我遵循了此link上的指南

这是我的用于生成签名的JSON Web令牌的代码:

    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("[email protected]")
                .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生成access_token时>

 curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=<signed_JWT>' https://oauth2.googleapis.com/token

我收到此错误:

{
    "error": "invalid_scope",
    "error_description": "Invalid oauth scope or ID token audience provided."
}

有任何提示吗?

我想使用从服务器生成的帐户服务令牌来调用Google API Analytics。我遵循了此链接上的指南。这是我的代码,用于生成签名的JSON Web令牌:final ...

google-api jwt google-oauth2 jwt-auth
1个回答
0
投票

使用echo "${SIGNED_JWT}" | cut -d. -f2 | base64 --decode解码JWT断言声明集,您会看到没有scope属性。

© www.soinside.com 2019 - 2024. All rights reserved.