Can't get the list of accounts using Google Contacts API & OAuth 2.0

时光毁灭记忆、已成空白 提交于 2019-12-12 03:41:09

问题


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

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