无法使用Google Contacts API& amp;获取帐户列表OAuth 2.0 [英] Can't get the list of accounts using Google Contacts API & OAuth 2.0

查看:91
本文介绍了无法使用Google Contacts API& amp;获取帐户列表OAuth 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索Google用户的联系人列表. 此getContacts()在AsyncTask的doInBackground()中调用. 至此,我已经收到了令牌. 我以以下codeLab示例为基础: https://codelabs. developers.google.com/codelabs/appauth-android-codelab/#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

我正在修改本教程的第10点,因此尝试获取用户的联系人列表而不是用户的个人信息(<起作用)

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

有帮助吗? 预先感谢

推荐答案

您可以参考

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);

也基于此帖子service.setOAuth2Credentials()不会像Google Sites,Google Drive等中的其他服务那样在内部刷新令牌.如果要构建Google凭据,只需在构建Google凭据后添加以下行:googleCredential.refreshToken();.

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

这篇关于无法使用Google Contacts API&amp; amp;获取帐户列表OAuth 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆