Search code examples
javaoauth-2.0google-apigdata

gdata oauth2 authorization NullPointerException: No authentication header information


I am trying to export/import google contacts using gdata+Oauth2 in my web application. The application has a client side on js and java server side, communicating through REST API. The js side performs auathorization via google getting the following data

{  
   state:"38c4ebb6-b763-4e98-969c-16a86221ec71",
   access_token:"ya29.BwEGCaDeWTzGqIwewwlmWreAMZdgNNexN1efOVGDcyY0f-gzXUot51F-Tzy5BX39CwGpbrL3JGjQ",
   token_type:"Bearer",
   expires_in:"3600"
}

I am trying to use access_token to get the contacts in the following way

ContactsService myService = new ContactsService(APP_NAME);
myService.setHeader("Authorization", "Bearer " + accessToken);
return GoogleDataUtils.getContactList(getContactFeed(myService));

where

private ContactFeed getContactFeed(ContactsService myService) throws ServiceException, IOException {
        URL feedUrl = new URL(URL_FOR_FEED);
        Query myQuery = new Query(feedUrl);
        myQuery.setMaxResults(Integer.MAX_VALUE);
        ContactFeed resultFeed = myService.getFeed(myQuery, ContactFeed.class);
        return resultFeed;
    }

But what I get is

Exception in thread "main" java.lang.NullPointerException: No authentication header information
    at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)
    at com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67)
    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608)
    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
    at com.google.gdata.client.Service.getFeed(Service.java:1135)
    at com.google.gdata.client.Service.getFeed(Service.java:1077)
    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676)
    at com.google.gdata.client.Service.getFeed(Service.java:1034)

I've found a resolved question for SpreadsheetService

gdata-java-client + oauth2 + access_token secret

But it did not work for me.

Could you please point me to what am I doing wrong? Any help will be appreciated

Thanks


Solution

  • The problem was in scopes onn the JS side - it was not set. Tryed on Oauth playground by Google, got a token and simply hardcoded it - it worked. After adding the scope to JS side (any JS Oauth library supports it) I succeded on authenticate. I advice everyone who faces such problem try to authenticate with token generated on Oauth playground, that will help to troubleshoot and find the problem. Thanks