问题
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
回答1:
Did you tried this solution?
How to get gmail user's contacts?
In addition, did you put in the Manifest all the permission you need? Did you defined properly you'r sha1 in the developer console?
回答2:
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();
.
来源:https://stackoverflow.com/questions/43050496/cant-get-the-list-of-accounts-using-google-contacts-api-oauth-2-0