在G-suite Admin SDK中获取所有已暂停用户的详细信息 [英] Fetch the details of all suspended users in G-suite Admin SDK

查看:84
本文介绍了在G-suite Admin SDK中获取所有已暂停用户的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to extract the details of all suspended users in G-Suite Admin SDK, for which I wrote a query in Google Apps script:

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\""}

我无法在响应中看到用户列表,如

I am not able to see the users list in the response as mentioned in G-suite-admin-SDK, which says the response should be something like:

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

我使用了搜索用户文档对于说要使用isSuspended=true的搜索查询,我是否可以知道我做错了什么?

I used the search-users documentation for the search-query which says to use isSuspended=true, may I know what am I doing wrong?

推荐答案

根据您链接的API文档,查询参数必须为string.您提供的查询无效-query: isSuspended=true-因此不执行任何查询.

Per the API documentation you link, the query parameter is required to be a string. You supply an invalid query - query: isSuspended=true - and thus no query is performed.

您可能会对搜索"中的示例感到困惑面向用户" 使用看似原始"变量和参数的API文档-这是因为它提供的示例仍然需要进行URL编码:

You may be confused by the examples in the "Search for Users" API documentation that use seemingly "raw" variables and parameters - this is because the examples it gives still need to be URL-encoded:

示例

所有查询均使用users.list方法,该方法具有类似于以下内容的HTTP请求(为了便于阅读,包含了换行符):

Examples

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

获取 https://www.googleapis.com/admin/directory/v1/用户
?domain =主域
& query =查询参数

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

查询参数必须经过URL编码.例如,查询query=givenName:Jane*是URL编码为query=givenName%3AJane*.此页面上的所有示例均显示未编码的查询参数.客户端库会自动处理此URL编码.

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变量来帮助自己并改善代码,例如:

You can help yourself and improve your code by re-using an options variable, e.g:

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

客户库和Apps脚本

Apps脚本中的高级服务" 是Google API封装基础REST API的客户端库,因此您不需要对传递给它们的方法的参数执行URL编码.如果您决定不想使用客户端库,而是希望使用UrlFetchApp查询URL,则需要对查询字符串进行URL编码. (如果您想提出许多简单,快速,不相关的请求,并且客户端库不提供

Client Libraries & Apps Script

The "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.)

这篇关于在G-suite Admin SDK中获取所有已暂停用户的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆