且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Google Apps脚本创建POST正文

更新时间:2023-02-25 12:59:36

您正在使用 p>

  fetchArgs.method ='GET'; 

,但在Acl:insert页面找到这里表示:


HTTP请求

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


所以, p>

  fetchArgs.method ='POST'; 


I'm trying to create a Google Apps Script that adds a new owner to the user's Google calendar. The first code block below works correctly (returns the calendar ACL in JSON format). How can I add a new user to the acl using Google Apps Script? The second code block shows my attempt to insert a new rule into the 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"};
}

Here's the second code block that returns server error 400 ("parse error"):

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

You are using:

  fetchArgs.method = 'GET';

but at the Acl:insert page found here says this:

HTTP Request

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

So, it should be,

  fetchArgs.method = 'POST';