我有一个Java应用程序在本地机器上运行,该应用程序使用GCP资源,如存储和Pubsub。因此,为了利用这些资源,我获得了一个服务帐户,以允许我的应用程序与gcp资源通信。在开发过程中,我测试了gcp流,一切正常,甚至存储和pubsub,但是在将该代码部署到本地计算机后,我遇到了这个问题Caused by: com.google.auth.oauth2.GoogleAuthException: Error getting access token for service account: Connection reset, iss:
...我已经在文档中搜索了这个问题,但它对我没有多大帮助....这个错误特别是在我尝试将媒体上传到存储时发生...使用凭据的代码
public Gcp(String keyPath) {
try {
this.credentials = GoogleCredentials.fromStream(new FileInputStream(keyPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Boolean uploadMedia(String projectId, String bucketInputMedia, String objectName, byte[] media) {
Storage storage = StorageOptions.newBuilder().setCredentials(this.credentials)
.setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketInputMedia, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
Blob blob = storage.create(blobInfo, media);
Boolean exists = blob.exists();
if (exists) {
return true;
}else {
return false;
}
}
错误的堆栈跟踪
Caused by: com.google.auth.oauth2.GoogleAuthException: Error getting access token for service account: Connection reset, iss: gglobo-decupador-hdg-dev@appspot.gserviceaccount.com
at com.google.auth.oauth2.GoogleAuthException.createWithTokenEndpointIOException(GoogleAuthException.java:166)
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:541)
at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:257)
at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:254)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.auth.oauth2.OAuth2Credentials$RefreshTask.run(OAuth2Credentials.java:623)
at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:398)
at com.google.auth.oauth2.OAuth2Credentials$AsyncRefreshResult.executeIfNew(OAuth2Credentials.java:571)
at com.google.auth.oauth2.OAuth2Credentials.asyncFetch(OAuth2Credentials.java:220)
at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:170)
at com.google.auth.oauth2.ServiceAccountCredentials.getRequestMetadata(ServiceAccountCredentials.java:931)
at com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:96)
at com.google.cloud.http.HttpTransportOptions$1.initialize(HttpTransportOptions.java:161)
at com.google.cloud.http.CensusHttpModule$CensusHttpRequestInitializer.initialize(CensusHttpModule.java:120)
at com.google.api.client.http.HttpRequestFactory.buildRequest(HttpRequestFactory.java:91)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:423)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:287)
... 83 common frames omitted
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:113)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012)
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:536)
我知道谷歌使用Oauth来验证谷歌API,但显然我的服务帐户有所有必要的信息,Oauth流保持,让我感到困惑的是,代码在本地工作正常
1条答案
按热度按时间kiz8lqtg1#
您可以检查以下步骤来解决此问题:
1.检查系统时钟是否同步。
1.您应该重复使用第一次成功验证后获得的访问令牌。如果您之前的令牌尚未过期,您将收到无效授权错误。请将其缓存在某个位置,以便您可以重复使用它。
1.请确保您传递了访问令牌,并在失败时刷新令牌。当您运行时,它会将OAuth2刷新令牌转换为访问令牌并将其传递给服务。如果您传递了原始令牌,则如果不将其转换为短期访问令牌,则API访问是不可接受的。
1.您可能已达到刷新令牌限制。某些Google API有以下说明,您可以在此处找到:https://developers.google.com/compute/docs/authentication
您还可以查看文档Using OAuth 2.0 to Access Google APIs