我正在尝试使用Google Apps脚本从Google Apps Reporting API检索指定我发送XML请求的数据
我的目标是检索disk_space_report并将数据放入电子表格中,这样我就可以监控该电子表格中的磁盘空间并处理一些数据。
任何人都可以举例说明如何做这类事情?
先谢谢。
答案 0 :(得分: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"};
}
答案 1 :(得分: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>
使用您的Google Apps域更改YOURDOMAIN.COM。在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函数并在记录器内部,您应该获得原始磁盘使用情况统计信息以进行解析。