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

2
  • Why JS is performing authentication? If you have Java as server side, you should follow the Using OAuth2 for Web Apps flow. Anyway, what is the output of accessToken in the Java side? Commented Jan 27, 2015 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 Commented Feb 3, 2015 at 23:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Add this workaround to your ContactService init block

myService.getRequestFactory().setHeader("User-Agent", applicationName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.