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

我与影子孤独终老i 提交于 2019-12-08 11:44:45

问题


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

The result that I am getting in the logs is:

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

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


回答1:


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.

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:

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.

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

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




回答2:


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.



来源:https://stackoverflow.com/questions/50956705/fetch-the-details-of-all-suspended-users-in-g-suite-admin-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!