无法使用服务帐户访问Google REST Api

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

我需要使用Http客户端库访问带有服务帐户的Google's BigQuery REST Api Endpoints。所以,我尝试了Using OAuth 2.0 for Server to Server Applications文章中提供的代码

它在下面的行中给出了编译错误。

Algorithm.RSA256(null,privateKey)

代码如下:

GoogleCredential credential = GoogleCredential
            .fromStream(new FileInputStream("service_account.json"));

    PrivateKey privateKey = credential.getServiceAccountPrivateKey();
    String privateKeyId = credential.getServiceAccountPrivateKeyId();

    try {

        Algorithm algorithm = Algorithm.RSA256(null, privateKey);

        String signedJwt = JWT.create().withKeyId(privateKeyId)
                .withIssuer("[email protected]")
                .withSubject("[email protected]")
                .withAudience("https://firestore.googleapis.com/google.firestore.v1beta1.Firestore")
                .withIssuedAt(new Date(now)).withExpiresAt(new Date(now + 3600 * 1000L)).sign(algorithm);
    } catch (Exception e) {
        e.printStackTrace();
    }

注意:Algorithm.RSA256()接受java.security.interfaces.RSAPrivateKey,而Google Api返回java.security.PrivateKey

有人可以帮助我吗?

google-bigquery google-oauth2 google-authentication google-oauth-java-client
1个回答
0
投票

我在实现中发现了什么:RSAPrivateKey继承自PrivateKey,所以如果你施放它(可能先进行类型检查),那么你应该好好去!

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