1

我正在尝试使用 Google Apps 脚本从Google Apps Reporting API检索数据,这些数据指定我发送 XML 请求

我的目标是检索 disk_space_report 并将该数据放入电子表格中,以便我可以监视该电子表格中的磁盘空间并处理一些数据。

任何人都可以给我一个关于如何做那种事情的例子吗?

先谢谢了。

4

2 回答 2

1

这是我编写并用于获取 Google Apps 用户帐户报告的一小段代码。

function startHere(){
  var domain = UserManager.getDomain();
  var fDate = '2012-12-18';//Utilities.formatDate(new Date(), Session.getTimeZone(), 'yyyy-MM-dd');
  var url = 'https://www.google.com/hosted/services/v1.0/reports/ReportingData';

  //Build API request parameters
  var fetchArgs = googleOAuth_('Reporting', url);
  fetchArgs.method = 'POST';
  var rawXML = '<?xml version="1.0" encoding="UTF-8"?>'
      +'<rest xmlns="google:accounts:rest:protocol" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">'
      +'<type>Report</type>'
      +'<domain>'+domain+'</domain>'
      +'<date>'+fDate+'</date>'
      +'<page>1</page>'
      +'<reportType>daily</reportType>'
      +'<reportName>accounts</reportName>'
      +'</rest>';
  fetchArgs.payload = rawXML;
  fetchArgs.contentType = "application/xml";
  fetchArgs.headers = {"Content-type": "application/atom+xml charset=UTF-8"}; 

  //Fetch CSV data
  var csvData = UrlFetchApp.fetch(url, fetchArgs).getContentText();

  //Parse CSV data and make a 2D array
  var recs = csvData.split('\n');
  var data = []; //this is actual 2D data
  for(var i=0; i<recs.length-1; i++){
    var temp = recs[i].split(',');
    if(i==0) temp.push('percent_disk_usage');
    else{
      var usage = (parseInt(temp[5])*100)/(parseInt(temp[4])*1024*1024);
      temp.push(usage);
    }
    data.push(temp);
  }
//Write data to spreadsheet
}

function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey("anonymous");
  oAuthConfig.setConsumerSecret("anonymous");
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}
于 2012-12-19T10:34:26.793 回答
0

只是一个快速的样本。在您的脚本中创建一个名为 xmlRequest 的新 HTML 文件,其中包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<rest xmlns="google:accounts:rest:protocol"
    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
        <type>Report</type>
    <domain>YOURDOMAIN.COM</domain>
      <date>2012-12-01</date>
        <page>1</page>
        <reportType>daily</reportType>
        <reportName>accounts</reportName>
</rest>

将 YOURDOMAIN.COM 更改为您的 Google Apps 域。在 Code.gs 中粘贴以下代码:

/**
 * Script configuration
 */
var SCOPE = 'https://www.google.com/hosted/services/v1.0/reports/ReportingData';
var APPNAME = "disk_space_report";
var URL = 'https://www.google.com/hosted/services/v1.0/reports/ReportingData';

function testit() {
  // Generate the new entry from a template
  var template = HtmlService.createHtmlOutputFromFile("xmlRequest").getContent();
  var response = UrlFetchApp.fetch(URL,googleOAuth_('POST', template));
  Logger.log(response.getContentText());

}


/**
 * Google authentication loader
 * @param {String} method the HTTP method to use for the UrlFetch operation, possible values are: GET, POST, PUT, DELETE
 * @param {String} payload the payload to use if needed
 * @return {Object} configuration options for UrlFetch, including oAuth parameters
 */
function googleOAuth_(method, payload) {
  // Shared configuration for all methods
  var oAuthConfig = UrlFetchApp.addOAuthService(APPNAME);
  oAuthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope='+encodeURIComponent(SCOPE));
  oAuthConfig.setAuthorizationUrl('https://www.google.com/accounts/OAuthAuthorizeToken');
  oAuthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken');
  oAuthConfig.setConsumerKey('anonymous');
  oAuthConfig.setConsumerSecret('anonymous');

  // Detect the required method
  switch(method) {
    case "GET":
      return {oAuthServiceName:APPNAME, oAuthUseToken:'always'};
      break;
    case "POST":
      return {oAuthServiceName:APPNAME, oAuthUseToken:'always', payload: payload, contentType: 'application/atom+xml', method: "POST"};
      break;
    case "PUT":
      return {oAuthServiceName:APPNAME, oAuthUseToken:'always', payload: payload, contentType: 'application/atom+xml', method: "PUT"};
      break;
    case "DELETE":
      return {oAuthServiceName:APPNAME, oAuthUseToken:'always', method: "DELETE"};
      break;
    default:
      return {oAuthServiceName:APPNAME, oAuthUseToken:'always'};
      break;
  }
}

现在运行 testit 函数,在记录器中,您应该获取原始磁盘使用情况统计信息以进行解析。

于 2012-12-19T09:55:21.977 回答