Google api 无法使用Google凭据获取访问令牌

Google api 无法使用Google凭据获取访问令牌,google-api,google-oauth-java-client,Google Api,Google Oauth Java Client,我正在尝试使用我的服务帐户JSON文件获取访问令牌。但是,我无法获取访问令牌 GoogleCredential googleCredential = GoogleCredential.fromStream(new FileInputStream(serviceAccountJsonFile)); System.out.print(googleCredential.getAccessToken()); 我每次都得到null。 我能够在Python API中使用相同的凭据 如果我在这里遗漏了什么,

我正在尝试使用我的服务帐户JSON文件获取访问令牌。但是,我无法获取访问令牌

GoogleCredential googleCredential = GoogleCredential.fromStream(new FileInputStream(serviceAccountJsonFile));
System.out.print(googleCredential.getAccessToken());
我每次都得到
null
。 我能够在Python API中使用相同的凭据


如果我在这里遗漏了什么,有人能告诉我吗

因此,我通过在getToken之前添加以下命令来解决此问题:

credential.refreshToken()
您的代码如下所示:

GoogleCredential googleCredential = GoogleCredential.fromStream(new FileInputStream(serviceAccountJsonFile));
credential.refreshToken();
System.out.print(googleCredential.getAccessToken());
这在谷歌的网站上没有适当的记录,但添加后,效果非常好,结果以实际值显示

我面临的另一个错误是“com.google.common.io.ByteStreams.detain(Ljava/io/InputStream;)J”,为了解决这个问题,我必须纠正我使用的依赖关系:

<dependency>
 <groupId>com.google.api-client</groupId>
 <artifactId>google-api-client</artifactId>
 <version>1.31.1</version>
</dependency>

com.google.api-client


我希望这对你也有帮助,伙计

我也面临同样的问题。你成功了吗。让它使用以下代码:
GoogleCredential GoogleCredential=GoogleCredential.fromStream(新文件输入流(serviceAccountJsonFile)、GoogleNethtTransport.newTrustedTransport()、JacksonFactory.getDefaultInstance())我在我的
GoogleCredential
对象中没有方法
GetAccessToken()
。我的是基于服务帐户的。有什么想法吗?