我收到错误
com.google.gdata.util.AuthenticationException: Unknown authorization header
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:600) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535) ~[gdata-core-1.0.jar:na]
尝试通过 API 访问 Google 日历数据时。
以下是该错误之前发生的情况。
1) 我通过 Google 进行身份验证:
final AccessTokenResponse response =
new GoogleAuthorizationCodeGrant(httpTransport,
jsonFactory,
clientId, clientSecret, authorizationCode,
redirectUrl).execute();
final GoogleAccessProtectedResource accessProtectedResource =
new GoogleAccessProtectedResource(
response.accessToken, httpTransport, jsonFactory,
clientId, clientSecret,
response.refreshToken);
LOGGER.debug("response.accessToken: {}", response.accessToken);
this.oauthAccessToken = response.accessToken;
...
2) 我通过任务 API 读取了一些数据:
this.service =
new Tasks(httpTransport, accessProtectedResource,
jsonFactory);
this.service.setApplicationName(this.applicationName);
这似乎有效。
3) 然后我尝试从 Google Calendar API 读取数据:
final OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
final GoogleOAuthParameters oauth = new GoogleOAuthParameters ();
oauth.setOAuthConsumerKey("myapp.com");
oauth.setOAuthConsumerSecret(CLIENT_SECRET); // Client secret from "Google API access" page, "Client secret" entry
oauth.setOAuthToken(this.oauthAccessToken); // Access token from step 1
oauth.setOAuthTokenSecret(aAuthorizationCode);
// aAuthorizationCode is taken from the callback URL.
// For http://myapp.com/oauth2callback?code=4/uy8Arb4bhRPwWYSr3QwKPt9lIZkt
// aAuthorizationCode is equal to "4/uy8Arb4bhRPwWYSr3QwKPt9lIZkt" (without quotes)
oauth.setScope(SCOPE_CALENDAR); // https://www.google.com/calendar/feeds/
final CalendarService calendarService =
new CalendarService(APPLICATION_NAME);
calendarService
.setOAuthCredentials(oauth, signer);
LOGGER.debug("calendarService: {}", calendarService);
final URL feedUrl =
new URL(
"http://www.google.com/calendar/feeds/default/allcalendars/full");
final CalendarFeed resultFeed =
calendarService.getFeed(feedUrl, CalendarFeed.class);
在最后一行(calendarService.getFeed...)发生上述异常。
我有以下问题:
1) 由我决定
oauth.setOAuthConsumerKey
正确吗?
我。 e. “消费者 key ”是否等于 Google API 控制台中的“产品名称”,或者等于“客户端 ID”字段(值类似于 42912397129473.apps.googleusercontent.com)
2) setOAuthTokenSecret 是否正确? IE。当 Google 将用户重定向回我的应用程序时,这是我收到的代码吗?
3) 如果问题 2 和 3 的回答为"is",那么还有什么原因导致我的问题?
谢谢
德米特里
P。 S.:以前,我可以通过简单的访问(即使用 Google 用户名和密码)来访问 Google 日历。不过,现在这不是一个选择,因为我的应用的用户不想泄露他们的 Google 密码。
最佳答案
最后,我按照以下示例解决了我的问题
http://code.google.com/p/gdata-java-client/source/browse/trunk/java/sample/oauth/OAuthExample.java
我对所有 future 受害者的建议^W OAuth 用户:注意 OAuth 教程中最微小的细节。 OAuth 的魔鬼在于细节。
关于java - Google 日历 API 和 OAuth 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550143/