我們使用谷歌聯繫人API與OAuth2用戶:谷歌聯繫人API - 無法刷新訪問令牌
credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(OAuth2ClientId(), OAuth2ClientSecret())
.addRefreshListener(new CredentialRefreshListener() {...});
myService = new ContactsService("My-App");
myService.setOAuth2Credentials(credential);
而且相當經常收到「401未授權」的響應,該庫的GData無法處理。 WWW-Authenticate
標題丟失時,AuthenticationException將拋出NPE。
Caused by: java.lang.NullPointerException: No authentication header information
at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96) ~[gdata-core-1.0-1.47.1.jar:na]
at com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67) ~[gdata-core-1.0-1.47.1.jar:na]
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608) ~[gdata-core-1.0-1.47.1.jar:na]
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564) ~[gdata-core-1.0-1.47.1.jar:na]
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560) ~[gdata-core-1.0-1.47.1.jar:na]
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538) ~[gdata-core-1.0-1.47.1.jar:na]
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536) ~[gdata-core-1.0-1.47.1.jar:na]
at com.google.gdata.client.Service.getFeed(Service.java:1135) ~[gdata-core-1.0-1.47.1.jar:1.47.1]
at com.google.gdata.client.Service.getFeed(Service.java:1077) ~[gdata-core-1.0-1.47.1.jar:1.47.1]
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676) ~[gdata-core-1.0-1.47.1.jar:1.47.1]
at com.google.gdata.client.Service.query(Service.java:1237) ~[gdata-core-1.0-1.47.1.jar:1.47.1]
at com.google.gdata.client.Service.query(Service.java:1178) ~[gdata-core-1.0-1.47.1.jar:1.47.1]
我們設法添加了一個包裝器,該包裝器會在此NPE上嘗試令牌刷新。它有助於但後來還是有很多情況下,當清涼的失敗:
credential.refreshToken() == false
當我們運行中我們看到,executeRefreshToken()
是沒有例外執行的,而是返回tokenResponse==null
調試refreshToken()
。結果refreshToken()
返回false
並沒有理由被傳遞給聽衆
try {
TokenResponse tokenResponse = executeRefreshToken();
if (tokenResponse != null) {
setFromTokenResponse(tokenResponse);
for (CredentialRefreshListener refreshListener : refreshListeners) {
refreshListener.onTokenResponse(this, tokenResponse);
}
return true;
}
} catch (TokenResponseException e) {
boolean statusCode4xx = 400 <= e.getStatusCode() && e.getStatusCode() < 500;
// check if it is a normal error response
if (e.getDetails() != null && statusCode4xx) {
// We were unable to get a new access token (e.g. it may have been revoked), we must now
// indicate that our current token is invalid.
setAccessToken(null);
setExpiresInSeconds(null);
}
for (CredentialRefreshListener refreshListener : refreshListeners) {
refreshListener.onTokenErrorResponse(this, e.getDetails());
}
if (statusCode4xx) {
throw e;
}
}
return false;
我們的令牌始終是多個範圍:https://www.googleapis.com/auth/userinfo.email https://www.google.com/m8/feeds https://www.googleapis.com/auth/calendar https://mail.google.com/ https://www.googleapis.com/auth/tasks
澄清,有時刷新工程,但有時它不?一旦刷新停止工作,它是否再次工作? AKA,這是暫時的還是永久的? – 2014-10-13 21:02:30
好的,我們一直在嘗試更多。聯繫人與日曆和任務的行爲似乎有點不同。它看起來像日曆和任務憑證。refreshToken()可能會失敗,沒有理由,但如果你等待,再試一次,它可能實際上刷新令牌。對於聯繫人來說,它是NPE「沒有驗證頭信息」,但是如果在某些情況下再次嘗試,它最終會刷新令牌,而在其他情況下它總是失敗。 – Alexey 2014-10-14 21:48:25
我正在複製問題。我當然看到NPE被拋出,但仍在努力使刷新失敗。 – 2014-10-16 15:07:21