使用 Google 脚本和人员 API 指定源覆盖同步令牌

Specifying sources overrides synctoken using Google Script and People API

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

<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"]

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

根据 documentation:

  1. sources[]

    Optional. A mask of what source types to return. Defaults to READ_SOURCE_TYPE_CONTACT and READ_SOURCE_TYPE_PROFILE if not set.

  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"] 更改了请求(也许您还更改了其他内容? ).