【问题标题】:Create a POST body using Google Apps Script使用 Google Apps 脚本创建 POST 正文
【发布时间】:2012-07-27 19:15:24
【问题描述】:

我正在尝试创建一个向用户的 Google 日历添加新所有者的 Google Apps 脚本。下面的第一个代码块可以正常工作(以 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); 
}

【问题讨论】:

  • 你觉得这个@KarBytes 怎么样?下面答案中电子邮件变量的编码是否解决了您添加用户的问题,还是您设法以其他方式解决了它?
  • 对 email 变量进行编码并没有改变任何东西。我认为 rawXML 可能需要标头。使用 API 版本 2 时出现服务器错误 404:var base = 'https://www.googleapis.com/calendar/v2/calendars/';

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


【解决方案1】:

在创建 rawXML 字符串时,我会像这样 encodeURIComponent(newUserEmail) 对 newUserEmail 变量进行编码,然后重试。

【讨论】:

    【解决方案2】:

    您正在使用:

      fetchArgs.method = 'GET';
    

    但在 Acl:insert 页面找到 here 说:

    HTTP 请求

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

    应该是这样的

      fetchArgs.method = 'POST';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2022-10-16
      相关资源
      最近更新 更多