简体   繁体   English

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

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

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: 我正在尝试提取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);
}

The result that I am getting in the logs is: 我在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: 我无法在响应中看到用户列表,如G-suite-admin-SDK所述 ,该响应表示响应应类似于:

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

I used the search-users documentation for the search-query which says to use isSuspended=true , may I know what am I doing wrong? 我使用了用于search-query的search-users文档,该文档说使用isSuspended=true ,请问我做错了什么吗?

Per the API documentation you link, the query parameter is required to be a string . 根据您链接的API文档,查询参数必须为string You supply an invalid query - query: isSuspended=true - and thus no query is performed. 您提供了一个无效查询query: isSuspended=true因此不执行任何查询。

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 : 您可能会对“搜索用户” API文档中使用看似“原始”变量和参数的示例感到困惑-这是因为其给出的示例仍需要进行URL编码

Examples 例子

All queries use the users.list method, which has an HTTP request similar to the following (line breaks included for readability): 所有查询都使用users.list方法,该方法具有类似于以下内容的HTTP请求(为了便于阅读,包含了换行符):

GET https://www.googleapis.com/admin/directory/v1/users GET https://www.googleapis.com/admin/directory/v1/users
?domain=primary domain ?domain =主域
&query=query parameters &query =查询参数

The query parameters must be URL encoded. 查询参数必须经过URL编码。 For example, the query query=givenName:Jane* is URL encoded as query=givenName%3AJane* . 例如,查询query=givenName:Jane*的URL编码为query=givenName%3AJane* All examples on this page show unencoded query parameters. 此页面上的所有示例均显示未编码的查询参数。 Client libraries handle this URL encoding automatically. 客户端库自动处理此URL编码。

You can help yourself and improve your code by re-using an options variable, eg: 您可以通过重新使用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;
}

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. Apps脚本中的“高级服务”是封装基础REST API的Google API客户端库,因此您无需对传递给其方法的参数执行URL编码。 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. 如果您决定不想使用客户端库,而是希望使用UrlFetchApp来查询URL,则需要对查询字符串进行URL编码。 (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 .) (如果要发出很多简单,快速,不相关的请求,并且客户端库不提供BatchHttpRequest方法,则可以执行此操作:可以使用UrlFetchApp.fetchAll以获得更好的性能 。)

It was a simple error: 这是一个简单的错误:

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

Thanks @tehhowch for helping me out. 感谢@tehhowch帮助我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批量更新用户G-suite - Bulk Update users G-suite 在 G-Suite 插件中获取 CalendarEvent 的重复发生 - Get recurrence of CalendarEvent in G-Suite Addon G套件商务邮件发送配额 - G-suite business mail sending quota 通过 G-Suite Marketplace SDK 发布时不会创建附加菜单,但会通过 Chrome 网上应用店创建 - Add-on menu doesn't get created when published through G-Suite Marketplace SDK, but does through Chrome Web Store 从G Suite管理员SDK删除用户帐户的脚本 - Script to delete a user account from G Suite Admin SDK 使用 App Script 和 Admin SDK 设置 G Suite 用户属性 - Setting G Suite user properties with App Script and the Admin SDK 使用G-Suite帐户登录自定义Web应用 - Signing into custom web app with G-Suite account 带有附件的Google G-Suite表单上的自定义验证 - custom validation on Google G-Suite Form with an add-on 事件保存/更新时的 g-suite Google 日历应用程序脚本 - g-suite Google Calender Apps script On Event Save/Update G-Suite 用户访问 G-Suite 可信应用脚本时,如何避免脚本授权提示? - How to avoid script authorization prompt when G-Suite user is accessing G-Suite trusted app script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM