gpt4 book ai didi

google-api - Google Contacts API - 无法刷新访问 token

转载 作者:行者123 更新时间:2023-12-04 09:16:42 24 4
gpt4 key购买 nike

我们将 Google Contacts API 与 OAuth2 结合使用:

credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(OAuth2ClientId(), OAuth2ClientSecret())
.addRefreshListener(new CredentialRefreshListener() {...});

myService = new ContactsService("My-App");
myService.setOAuth2Credentials(credential);
我们经常收到 GData 库无法处理的“401 Unauthorized”响应。
AuthenticationException 在 WWW-Authenticate 时抛出 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 上刷新 token 。它有帮助,但仍然有很多刷新失败的情况:
credential.refreshToken() == false
当我们运行时 refreshToken()在调试器中,我们看到 executeRefreshToken()无一异常(exception)地被执行,但 tokenResponse==null被退回。结果 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;
我们的 token 始终适用于多个范围: 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 更新:我们已成功迁移到 People API,我们的 Unified API Contacts API 也使用了新的 API https://docs.aurinko.io/article/25-contacts-api

最佳答案

当前在 Contacts API 中存在一个错误,其中某些 HTTP User-Agent字符串,导致 401 响应作为 HTML 页面而不是 XML 响应返回,并且缺少 WWW-Authenticate AuthenticationException 的标题类是靠的。 GContacts-Java是这些特殊的用户代理字符串之一。解决方法是在创建客户端后更改客户端的用户代理:

ContactsService service = new ContactsService(applicationName);
service.getRequestFactory().setHeader("User-Agent", applicationName);

这应该消除 NPE,并允许客户端库自动检测过期 token 并自动刷新它们。

关于google-api - Google Contacts API - 无法刷新访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26286369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com