简体   繁体   English

获取G Suite中给定用户列表的用户活动报告

[英]Getting User Activity Reports for a given list of users in G Suite

I'm trying to use the Admin SDK Reports Service to get the last login time and some other data for 20 users. 我正在尝试使用Admin SDK报告服务来获取20个用户的上次登录时间和其他一些数据。 It's not feasible to get the data for the whole domain and then filter down due to the size of the domain, so I want to get the data for only these 20 users, but I can't seem to find a way to do that. 获取整个域的数据然后由于域的大小而进行过滤是不可行的,因此我只想获取这20个用户的数据,但是我似乎找不到找到这种方法的方法。 My current code fails with Admin user usage reports get failed with error: GoogleJsonResponseException: API call to reports.userUsageReport.get failed with error: Bad Request 我当前的代码失败, Admin user usage reports get failed with error: GoogleJsonResponseException: API call to reports.userUsageReport.get failed with error: Bad Request

I know this is wrong but looking at the docs I'm not sure what to do. 我知道这是错误的,但是在查看文档时,我不确定该怎么做。 I can use one user and that works fine on the website, but it seems weird that the only option would be to get all users or one user. 我可以使用一个用户,并且可以在网站上正常工作,但是似乎唯一的选择就是让所有用户或一个用户来使用,这很奇怪。 I tried to modify my code to get one user multiple times in a loop, but fails with the above error. 我试图修改我的代码以在一个循环中多次获得一个用户,但是由于上述错误而失败。 the userList value is pulled from a table in the AppMaker user interface. userList值是从AppMaker用户界面中的表中提取的。 I can also use the documentation website's API explorer with no problems. 我也可以毫无问题地使用文档网站的API资源管理器。

Here's my current function: 这是我当前的功能:

function generateLoginActivityReport(userList) {
  var today = new Date();
  var oneWeekAgo = new Date(today.getTime() - (7 * 24 * 60 * 60 * 1000));
  var timezone = Session.getScriptTimeZone();
  var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');

  userList = JSON.parse(userList);
  //console.log("server code: " + list);

  for (var a = 0; a < userList.length; a++) {
    //console.log(userList[a]);
    var parameters = [
      'accounts:last_login_time',
      'drive:num_items_created'
    ];
    var rows = [];
    var pageToken;
    var page;
    do {
    page = AdminReports.UserUsageReport.get('all', date, {
      parameters: parameters.join(','),
      maxResults: 1,
      pageToken: pageToken,
      userKey: userList[a]
    });

    var reports = page.usageReports;

    if (page.warnings) {
      for (var q = 0; q < page.warnings.length; q++) {
        var warning = page.warnings[a];
        Logger.log(warning.message);
      }
    }

    if (reports) {
      for (var i = 0; i < reports.length; i++) { 
        var report = reports[i];
        try {
          var parameterValues = getParameterValues(report.parameters);
          var row = [
            report.date,
            report.entity.userEmail,
            parameterValues['accounts:last_login_time'],
            //parameterValues['drive:num_items_created']
          ];
          rows.push(row);
          var ar = app.models.ActivityReport.newRecord();
          ar.LastLogin = parameterValues['accounts:last_login_time'];
          console.log(report.entity.userEmail);
          ar.DocsAdded = 0; //getting this value is another issue but unrelated so it's set to 0 for now.
          ar.Email = report.entity.userEmail.toString();
          app.saveRecords([ar]);
        }
        catch(error) {
          console.error("Error! Did not write last item to model: \n"+error);
        }
      }
    }
  } while (pageToken);
  }
}

The problem is caused by the way how you call the method AdminReports.UserUsageReport() . 问题是由您如何调用方法AdminReports.UserUsageReport() The right syntax is: 正确的语法是:

AdminReports.UserUsageReport.get(userKey, date, optionalArgs) 

Thus, you need to substitute the userkey 'all' through userList[a] , rather than inserting the userkey in the parameters: 因此,您需要通过userList[a]替换用户密钥'all' ,而不是在参数中插入用户userkey

page = AdminReports.UserUsageReport.get(userList[a], date, {
  parameters: parameters.join(','),
  maxResults: 1,
  pageToken: pageToken,
});

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

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