简体   繁体   中英

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:

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?

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, eg:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM