Google Apps Profiles API: java.lang.NullPointerException: No authentication header information

2.6k views Asked by At
public ContactFeed retrieveContacts(ContactsService service,
                                    Credential credential) throws Exception {

    URL url = new URL("https://www.google.com/m8/feeds/profiles/domain/my.domain.com/full");

    service.setOAuth2Credentials(credential);
    service.useSsl();
    service.setHeader("GData-Version", "3.0");

    ContactFeed contactFeed = new ContactFeed();

    do {
        ContactFeed feedResult = service.getFeed(url, ContactFeed.class);
        contactFeed.getEntries().addAll(feedResult.getEntries());

        if (feedResult.getNextLink() != null && feedResult.getNextLink().getHref() != null
                && !feedResult.getNextLink().getHref().isEmpty()) {
            url = new URL(feedResult.getNextLink().getHref());
        } else {
            url = null;
        }
    } while (url != null);

    return contactFeed;
}

This is my code for retrieving profiles. When running this line

ContactFeed feedResult = service.getFeed(query, ContactFeed.class);

It will give java.lang.NullPointerException: No authentication header information error.

I have Google Sites, Google Drive, Google Apps Provisioning, Google Email Settings run well with OAuth2. But Google Apps Profile with OAuth2 gives java.lang.NullPointerException: No authentication header information. I surfed around internet, they said this is a bug? All they did is to manually pass the AccessToken into the header. Is there anyway for this work around? Because I am using this to run at Cron Job and I am using OAuth2 service account.

====

Edit:

I tried to brute-force it with set header like this:

service.setHeader("GData-Version", "3.0");
service.setUserCredentials(myusername, mypassword);

This works just perfect. service.setOAuth2Credentials() seems having bug internally.

2

There are 2 answers

0
lannyboy On

I found a reason why service.setOAuth2Credentials() doesn't work with building new Google Credential (build with P12File).

It is because service.setOAuth2Credentials() doesn't REFRESH TOKEN internally like the other services in Google Sites, Google Drive and etc...

If you build Google Credential with P12File, just add this line after building the Google Credential.

googleCredential.refreshToken();

I guess it is a bug for ContactsService.setOAuth2Credentials().

0
Amélie Medem On

I had this problem while using SecureSociale for the authentification.

In my case, the issue was related to the scope defined in the configuration of SecureSociale.

Don't forget to check that your are using the right scope for the given token.