Google OAuth Credential.refreshToken 上的连接重置
Connection Reset on Google OAuth Credential.refreshToken
我正在尝试使用服务帐户创建凭据(以便检索 Google 分析信息)。
但是当 Credential.refreshToken(..) 被调用时,我得到一个
java.net.SocketException: 连接重置。
我已经使用 Google 开发者控制台创建了一个 OAuth 服务帐户和一个 P12 文件。
然后我执行下面的代码:
// Create the credential
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
File p12File = new File(System.getProperty("user.home"), ".store/analytics/my-project.p12");
Set<String> serviceAccountScopes = Collections.singleton(AnalyticsScopes.ANALYTICS);
GoogleCredential.Builder builder = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("123abc<etc>@developer.gserviceaccount.com")
.setServiceAccountPrivateKeyFromP12File(p12File)
.setServiceAccountScopes(serviceAccountScopes);
GoogleCredential credential = builder.build();
// Attempt to refresh the credential's token
credential.refreshToken();
这会导致短暂的停顿,然后出现以下异常:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
at sun.security.ssl.InputRecord.read(InputRecord.java:480)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
at sun.security.ssl.SSLSocketImpl.waitForClose(SSLSocketImpl.java:1705)
at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:122)
at sun.security.ssl.Handshaker.kickstart(Handshaker.java:909)
at sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1423)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1288)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
<etc>
代码正在 Tomcat 上的 Web 应用程序 运行 中执行。
非常感谢任何对此问题的帮助!
对于到达此处的任何其他人...
问题是工作中的某种防火墙问题。当我通过 phone 创建 Wifi 热点并完成它时,一切都运行良好。
我正在尝试使用服务帐户创建凭据(以便检索 Google 分析信息)。
但是当 Credential.refreshToken(..) 被调用时,我得到一个
java.net.SocketException: 连接重置。
我已经使用 Google 开发者控制台创建了一个 OAuth 服务帐户和一个 P12 文件。 然后我执行下面的代码:
// Create the credential
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
File p12File = new File(System.getProperty("user.home"), ".store/analytics/my-project.p12");
Set<String> serviceAccountScopes = Collections.singleton(AnalyticsScopes.ANALYTICS);
GoogleCredential.Builder builder = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("123abc<etc>@developer.gserviceaccount.com")
.setServiceAccountPrivateKeyFromP12File(p12File)
.setServiceAccountScopes(serviceAccountScopes);
GoogleCredential credential = builder.build();
// Attempt to refresh the credential's token
credential.refreshToken();
这会导致短暂的停顿,然后出现以下异常:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
at sun.security.ssl.InputRecord.read(InputRecord.java:480)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
at sun.security.ssl.SSLSocketImpl.waitForClose(SSLSocketImpl.java:1705)
at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:122)
at sun.security.ssl.Handshaker.kickstart(Handshaker.java:909)
at sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1423)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1288)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
<etc>
代码正在 Tomcat 上的 Web 应用程序 运行 中执行。
非常感谢任何对此问题的帮助!
对于到达此处的任何其他人...
问题是工作中的某种防火墙问题。当我通过 phone 创建 Wifi 热点并完成它时,一切都运行良好。