在 G-suite Admin SDK 中获取所有暂停用户的详细信息

Fetch the details of all suspended users in G-suite Admin SDK

我正在尝试提取 G-Suite Admin SDK 中所有暂停用户的详细信息,为此我在 Google Apps 脚本中编写了一个查询:

function fetchUser(){           
    var pageToken;
    var membersList = AdminDirectory.Users.list({
      domain: 'xyz.com',
      orderBy: 'email',
      query: isSuspended=true,
      maxResults: 100,
      pageToken: pageToken
    });  
    Logger.log('membersList:' +membersList);
}

我在 logs 中得到的结果是:

[18-06-20 16:32:15:413 EDT] membersList:{"kind":"admin#directory#users","etag":"\"npJcgeAc7XbfkhvPm3glLSpkcPU/HMFwD2wLX237BRmKZQUJYB5ZE7U\""}

我无法看到 G-suite-admin-SDK 中提到的响应中的用户列表,它表示响应应该类似于:

{
  "kind": "admin#directory#users",
  "etag": etag,
  "users": [
    users Resource
  ],
  "nextPageToken": string
}

我在搜索查询中使用了 search-users 文档,它说要使用 isSuspended=true,请问我做错了什么吗?

这是一个简单的错误:

function fetchUser(){           
        var pageToken;
        var membersList = AdminDirectory.Users.list({
          domain: 'xyz.com',
          orderBy: 'email',
          query: "isSuspended=true",
          maxResults: 100,
          pageToken: pageToken
        });  
        Logger.log('membersList:' +membersList);
    }

感谢@tehhowch 帮助我。

根据您 link 的 API 文档,查询参数必须是 string。您提供了一个无效查询 - query: isSuspended=true - 因此没有执行任何查询。

您可能会对 "Search for Users" API 文档中使用看似 "raw" 变量和参数的示例感到困惑 - 这是因为它给出的示例 仍然需要 URL-编码:

Examples

All queries use the users.list method, which has an HTTP request similar to the following (line breaks included for readability):

GET https://www.googleapis.com/admin/directory/v1/users
?domain=primary domain
&query=query parameters

The query parameters must be URL encoded. For example, the query query=givenName:Jane* is URL encoded as query=givenName%3AJane*. All examples on this page show unencoded query parameters. Client libraries handle this URL encoding automatically.

您可以通过重新使用 options 变量来帮助自己改进代码,例如:

function getAllSuspended() {
  // Set the constant options only once.
  const options = {
    domain: 'xyz.com',
    orderBy: 'email',
    query: 'isSuspended=true',
    maxResults: 100,
    fields: "nextPageToken,users"
  };
  // Could log the options here to ensure they are valid and in the right format.

  const results = [];
  do {
    var search = AdminDirectory.Users.list(options);
    // Update the page token in case we have more than 1 page of results.
    options.pageToken = search.nextPageToken;
    // Append this page of results to our collected results.
    if(search.users && search.users.length)
      Array.prototype.push.apply(results, search.users);
  } while (options.pageToken);

  return results;
}

客户端库和应用程序脚本

"advanced services" in Apps Script are Google API client libraries that wrap the underlying REST API, so you do not need to perform the URL encoding on parameters you pass to their methods. If you decided you didn't want to use the client library, preferring to query the URL with UrlFetchApp, then you would need to URL encode the querystring. (You might do this if you wanted to make a lot of simple, quick, unrelated requests and the client library does not offer a BatchHttpRequest method: you could use UrlFetchApp.fetchAll for better performance.)