Google Calendar API and OAuth problem

和自甴很熟 提交于 2019-12-22 10:11:12

问题


I get the error

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]

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

Here is what happens before that error.

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) I read some data via the tasks API:

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

This seems to work.

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);

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

I have following questions:

1) Is my call

oauth.setOAuthConsumerKey

correct?

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) Is the setOAuthTokenSecret correct? I. e. is it the code that I get, when Google redirects the user back to my app?

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

Thanks

Dmitri

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/6550143/google-calendar-api-and-oauth-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!