【问题标题】:Google Calendar API and OAuth problemGoogle Calendar API 和 OAuth 问题
【发布时间】:2011-07-03 21:38:56
【问题描述】:

我得到了错误

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.是等于 Google API 控制台中的“产品名称”或“客户端 ID”字段的“消费者密钥”(值类似于 42912397129473.apps.googleusercontent.com)

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

3) 如果问题 2 和 3 的回答是肯定的,我的问题还有什么原因?

谢谢

德米特里

P。 S.:以前,我可以通过简单的访问(即使用 Google 用户名和密码)访问 Google 日历。但是,现在这不是一个选项,因为我的应用程序的用户不想泄露他们的 Google 密码。

【问题讨论】:

    标签: java oauth google-calendar-api oauth-2.0


    【解决方案1】:

    最后,我通过以下示例解决了我的问题

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

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

    【讨论】:

    • 意思是,如果应用程序是使用返回客户端ID的Google API创建的,您还需要在google.com/accounts/ManageDomains注册您的域以获得消费者密钥??谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多