0

I'm trying to retrieve a Google user's contacts list. This getContacts() is called in the doInBackground() of an AsyncTask. I have -at this point- already received a token. I based my self on this codeLab sample : https://codelabs.developers.google.com/codelabs/appauth-android-codelab/#0

I'm modifying the point n°10 of this tutorial so trying to fetch user's contact list instead of the user's personal info ( < which is working)

private List<ContactEntry> getContacts() {
        ContactsService contactsService = new ContactsService("MY_PRODUCT_NAME");
        contactsService.setHeader("Authorization", "Bearer " + token);

        try {
            URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
            Query myQuery = new Query(feedUrl);
            ContactFeed resultFeed = contactsService.query(myQuery, ContactFeed.class);
            List<ContactEntry> contactEntries = resultFeed.getEntries();
            return contactEntries;
        } catch (Exception e) {
        }
        return null;
    }

My problem is that I always get an Exception with this message :

java.lang.NullPointerException: No authentication header information

Any help? thanks in advance

Greg
  • 689
  • 2
  • 8
  • 23

1 Answers1

0

You may refer with this related thead. Try modifying the client library's user agent:

A workaround is to change the user agent of your client after you create it:

ContactsService service = new ContactsService(applicationName);
service.getRequestFactory().setHeader("User-Agent", applicationName);

Also based from this post, service.setOAuth2Credentials() doesn't refresh token internally like the other services in Google Sites, Google Drive and etc. If you build Google Credential, just add this line after building the Google Credential: googleCredential.refreshToken();.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59