【问题标题】:Can't get the list of accounts using Google Contacts API & OAuth 2.0无法使用 Google Contacts API 和 OAuth 2.0 获取帐户列表
【发布时间】:2017-03-27 15:27:34
【问题描述】:

我正在尝试检索 Google 用户的联系人列表。 此 getContacts() 在 AsyncTask 的 doInBackground() 中调用。 我已经 - 在这一点上 - 已经收到了一个令牌。 我以这个 codeLab 示例为基础:https://codelabs.developers.google.com/codelabs/appauth-android-codelab/#0

我正在修改本教程的第 10 点,以便尝试获取用户的联系人列表而不是用户的个人信息(

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

我的问题是我总是收到一条异常消息:

java.lang.NullPointerException: No authentication header information

有什么帮助吗? 提前致谢

【问题讨论】:

    标签: android oauth-2.0 google-contacts-api


    【解决方案1】:

    您可以参考此related thead。尝试修改客户端库的用户 代理:

    一种解决方法是在创建客户端后更改它的用户代理:

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

    同样基于此postservice.setOAuth2Credentials() 不会像 Google Sites、Google Drive 等中的其他服务那样在内部刷新令牌。如果您构建 Google Credential,只需在构建 Google Credential 后添加此行:@ 987654325@.

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 2015-03-30
      • 2021-12-25
      • 2012-02-12
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      相关资源
      最近更新 更多