1

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

Community
  • 1
  • 1
lopushen
  • 1,117
  • 2
  • 11
  • 32
  • Why JS is performing authentication? If you have Java as server side, you should follow the [Using OAuth2 for Web Apps](https://developers.google.com/accounts/docs/OAuth2WebServer) flow. Anyway, what is the output of `accessToken` in the Java side? – Rael Gugelmin Cunha Jan 27 '15 at 10:13
  • The JS side is performing oauth because it is just a front-end side, so I have no possibility to do oauth on server which just provides REST api. As far as I know it is a valid solution as far as token transfer is secured – lopushen Feb 03 '15 at 23:44

2 Answers2

3

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

lopushen
  • 1,117
  • 2
  • 11
  • 32
0

Add this workaround to your ContactService init block

myService.getRequestFactory().setHeader("User-Agent", applicationName);
user3686724
  • 603
  • 1
  • 5
  • 15