使用Google Apps脚本创建POST正文

时间:2012-07-27 19:15:25

标签: post google-apps-script google-calendar-api urlfetch

我正在尝试创建一个Google Apps脚本,为用户的Google日历添加新的所有者。下面的第一个代码块正常工作(以JSON格式返回日历ACL)。如何使用Google Apps脚本向acl添加新用户?第二个代码块显示我尝试将新规则插入到acl中。

function getCalendarACL() {

  // Get Calendar ID, script user's email, and the API Key for access to Calendar API
  var calId = 'abc123@group.calendar.google.com';
  var userEmail = Session.getActiveUser().getEmail();
  var API_KEY = '012345abc123';  

  // Get authorization to access the Google Calendar API
  var apiName = 'calendar';
  var scope = 'https://www.googleapis.com/auth/calendar';
  var fetchArgs = googleOAuth_(apiName, scope);

  // Get the authorization information and the given calendar
  fetchArgs.method = 'GET';

  // Get the requested content (the ACL for the calendar)
  var base = 'https://www.googleapis.com/calendar/v3/calendars/';
  var url = base + calId + '/acl?key=' + API_KEY;
  var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
  Logger.log(content); 
}

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

这是返回服务器错误400的第二个代码块(“解析错误”):

 function insertRule() {

  // Get Calendar ID, script user's email, and the API Key for access to Calendar API
  var calId = 'abc123@group.calendar.google.com';
  var userEmail = Session.getActiveUser().getEmail();
  var API_KEY = '012345abc123'; 
  var newUserEmail = 'person@gmail.com'; 

  // Get authorization to access the Google Calendar API
  var apiName = 'calendar';
  var scope = 'https://www.googleapis.com/auth/calendar';
  var fetchArgs = googleOAuth_(apiName, scope);

  // Get the authorization information and the given calendar
  fetchArgs.method = 'GET';

  // Create the POST request body
  var rawXML = "<entry xmlns='http://www.w3.org/2005/Atom' " +
           "xmlns:gAcl='http://schemas.google.com/acl/2007'>" +
           "<category scheme='http://schemas.google.com/g/2005#kind'" +
           "term='http://schemas.google.com/acl/2007#accessRule'/>" +
           "<gAcl:scope type='user' value='"+newUserEmail+"'></gAcl:scope>" +
           "<gAcl:role='writer'>" +
           "</gAcl:role>" +
           "</entry>";

  // Get the requested content (the ACL for the calendar)
  var base = 'https://www.googleapis.com/calendar/v3/calendars/';
  var url = base + calId + '/acl?key=' + API_KEY;
  var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
  Logger.log(content); 
}

2 个答案:

答案 0 :(得分:1)

在编写rawXML字符串然后重试时,我会像这个encodeURIComponent(newUserEmail)一样编码newUserEmail变量。

答案 1 :(得分:1)

您正在使用:

  fetchArgs.method = 'GET';

但是在找到的Acl:insert页面中here说明了这一点:

  

HTTP请求

POST https://www.googleapis.com/calendar/v3/calendars/calendarId/acl

所以,它应该是,

  fetchArgs.method = 'POST';