【问题标题】:Google Contacts API v3 suddenly retrieving only 1.500 conctactsGoogle Contacts API v3 突然只检索到 1.500 个联系人
【发布时间】:2020-05-16 07:21:08
【问题描述】:

我已经使用这个 API 6 年了,没有任何问题,但是从 3 天前开始,我经历了一系列奇怪的行为,意识到我的 google 联系人开始不受控制地复制。 我发现它与检索联系人时对 1.500 的奇怪限制有关,即使我在 GET 查询中使用 max-results。

这是我的代码(PHP 服务器):

$req = new Google_Http_Request("https://www.google.com/m8/feeds/contacts/".$user_email."/full?max-results=1000000");
$req->setRequestHeaders(array('GData-Version'=> '3.0','content-type'=>'application/atom+xml; charset=UTF-8; type=feed'));
$auth = $client->getAuth();
$val = $auth->authenticatedRequest($req);
$response = $val->getResponseBody();
$xml = simplexml_load_string($response);

结果是 $xml->entry 只有 1.500 个元素,即使联系人是 8.000 个。

有人知道发生了什么吗?

【问题讨论】:

    标签: google-contacts-api


    【解决方案1】:

    我联系了我正在使用的联系人同步脚本的作者,似乎还有另一种方法有效 - 这是当前修订版的链接,以及 Adler 先生最近提交的似乎解决了该问题的提交。请记住,这是 python,但我想可以使用 PHP 进行类似的检查? https://github.com/michael-adler/sync-google-contacts https://github.com/michael-adler/sync-google-contacts/commit/4c5a8517e9da84d59769d8b513f5b637782aea14

    MAX_RESULTS 是之前设置的一个变量,10000,但也可能是 1500 或更少,因为这是我们得到的最大值。

        query = gdc.ContactsQuery(max_results=MAX_RESULTS)
        feed = self.gd_client.GetContacts(q=query)
        while feed:
    

    然后,检查更多联系人:

            next_link = feed.GetNextLink()
            if next_link:
                feed = self.gd_client.GetContacts(uri=next_link.href)
            else:
                feed = None
    

    如果您在 oauth2 Playground 中查看返回的 xml/json,您会看到响应包含一些有用的数据,您现在需要利用这些数据来完成这项工作。

    例如,如果您发送(授权)请求 https://www.google.com/m8/feeds/contacts/default/full?max_results=10

    响应包含

    <openSearch:totalResults>1234</openSearch:totalResults>
    

    这包含您的联系人数量。

    还有一堆url链接返回。

    我们需要的,如果存在,则表明有更多可用的联系人,由

    标识

    rel='下一个'

    示例:

     <link href='https://www.google.com/m8/feeds/contacts/username%40gmail.com/full?max-results=10' rel='self' type='application/atom+xml'/>
     <link href='https://www.google.com/m8/feeds/contacts/username%40gmail.com/full?start-index=11&amp;max-results=10' rel='next' type='application/atom+xml'/>
    

    start-index 是总数的联系号码,因此如果您将 max_results 设置为 1000,并且您有 3500 个联系人,您将需要发送您的初始请求,然后从响应中提取每个“下一个”url需要后续(在本例中为 3 个)请求,附加后续数据,以将所有联系人添加到您的数组中。

    您的初始查询可能是

    https://www.google.com/m8/feeds/contacts/username%40gmail.com/full?&max_results=1000
    

    您的后续请求将以

    结尾
    https://www.google.com/m8/feeds/contacts/username%40gmail.com/full?start-index=1001&max_results=1000
    https://www.google.com/m8/feeds/contacts/username%40gmail.com/full?start-index=2001&max_results=1000
    https://www.google.com/m8/feeds/contacts/username%40gmail.com/full?start-index=3001&max_results=1000 (would not return XML containing a link with rel='next' as this is the last page)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 2015-01-31
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多