Google Calendar API和OAuth问题 [英] Google Calendar API and OAuth problem

查看:70
本文介绍了Google Calendar API和OAuth问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了错误

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日历数据时.

when trying to access the Google Calendar data via their API.

这是该错误发生之前发生的情况.

Here is what happens before that error.

1)我向Google进行身份验证:

1) I authenticate with 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读取了一些数据:

2) I read some data via the tasks API:

    this.service =
            new Tasks(httpTransport, accessProtectedResource,
                    jsonFactory);
    this.service.setApplicationName(this.applicationName);

这似乎可行.

3)然后,我尝试从Google Calendar API中读取数据:

3) Then I try to read data from the 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 ...),发生上述异常.

At the last line (calendarService.getFeed...) the aforementioned exception occurs.

我有以下问题:

1)是我的电话

oauth.setOAuthConsumerKey

正确吗?

I. e.是在Google API控制台中等于产品名称"或在客户端ID"字段中的消费者密钥"(值类似于42912397129473.apps.googleusercontent.com)

I. e. is the "consumer key" equal to "Product name" in the Google API console, or to "Client ID" field (value is something like 42912397129473.apps.googleusercontent.com)

2)setOAuthTokenSecret是否正确? IE.当Google将用户重定向回我的应用时,是我得到的代码吗?

2) Is the setOAuthTokenSecret correct? I. e. is it the code that I get, when Google redirects the user back to my app?

3)如果对问题2和3的回答为是",那么还有什么原因可以引起我的问​​题?

3) If questions 2 and 3 were answered with yes, what else can be the cause of my problem?

谢谢

Dmitri

P. S .:以前,我可以通过简单的访问方式(即使用Google用户名和密码)访问Google日历.但是,由于我的应用程序的用户不想放弃自己的Google密码,因此现在无法选择此选项.

P. S.: Previously, I could access Google calendar with simple access (i. e. with Google user name and password). However, this is not an option now because users of my app will not want to give away their Google password.

推荐答案

最后,我按照以下示例解决了我的问题

Finally, I solved my problem by following the example at

http: //code.google.com/p/gdata-java-client/source/browse/trunk/java/sample/oauth/OAuthExample.java

我对所有将来的OAuth受害者^ W用户的建议:请注意OAuth教程中的最小细节. OAuth的魔鬼在于细节.

My advice to all future victims^W users of OAuth: Pay attention to the smallest details in the OAuth tutorials. The OAuth devil lies in details.

这篇关于Google Calendar API和OAuth问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆