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

笑着哭i 提交于 2019-12-10 10:04:26

问题


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.


回答1:


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().




回答2:


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.



来源:https://stackoverflow.com/questions/14350971/google-apps-profiles-api-java-lang-nullpointerexception-no-authentication-head

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