首页 > 解决方案 > 使用 Google Script 和 People API 指定来源会覆盖同步令牌

问题描述

我在https://developers.google.com/people/api/rest/v1/people.connections/list使用 Google 的“Try It”运行此代码。

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for people.people.connections.list
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/guides/code_samples#javascript
   */

  function authenticate() {
    return gapi.auth2.getAuthInstance()
        .signIn({scope: "https://www.googleapis.com/auth/contacts https://www.googleapis.com/auth/contacts.readonly"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }
  function loadClient() {
    return gapi.client.load("https://people.googleapis.com/$discovery/rest?version=v1")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.people.people.connections.list({
      "resourceName": "people/me",
      "personFields": "names",
      "sources": [
        "READ_SOURCE_TYPE_CONTACT"
      ],
      "syncToken": "MisAPB3nNAAAABIIsK7pgqKn8gIQsK7pgqKn8gI4IFedl0ChD-QAdeetUtBPOgw2ODIzOTA3NzkxNzA="
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

当我排除

"sources": ["READ_SOURCE_TYPE_CONTACT"]

从“执行”函数中,代码仅返回自上次设置同步令牌以来更改的联系人(我想要的结果)。但是,当我包含该行时,我的所有联系人都会返回。任何人都知道为什么会发生这种情况以及如何避免它(除了删除该行 - 我需要排除 DOMAIN 和 PROFILE 源)?谢谢!

标签: google-apps-scriptgoogle-people-api

解决方案


根据文档

  1. 来源[]

    选修的。要返回的源类型的掩码。如果未设置,则默认为 READ_SOURCE_TYPE_CONTACT 和 READ_SOURCE_TYPE_PROFILE。

  2. When the pageToken or syncToken is specified, all other request parameters must match the first call..

换句话说,一般来说,无论您设置"sources": ["READ_SOURCE_TYPE_CONTACT"]与否,它都不应该有所不同 - 因为sources自动默认为READ_SOURCE_TYPE_CONTACT.

API 未正确返回唯一新联系人的最可能原因是您通过排除该行更改了请求"sources": ["READ_SOURCE_TYPE_CONTACT"](也许您还更改了其他内容?)。


推荐阅读