本文介绍了使用Google Apps脚本创建POST正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  function getCalendarACL(){

//获取日历ID,脚本用户的电子邮件以及用于访问Calendar API的API密钥
var calId ='abc123@group.calendar.google.com';
var userEmail = Session.getActiveUser()。getEmail();
var API_KEY ='012345abc123';

//获取访问Google Calendar API的授权
var apiName ='calendar';
var scope ='https://www.googleapis.com/auth/calendar';
var fetchArgs = googleOAuth_(apiName,scope);

//获取授权信息和给定日历
fetchArgs.method ='GET';

//获取请求的内容(日历的ACL)
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);
}

函数googleOAuth_(name,scope){
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl(https://www.google.com/accounts/OAuthGetRequestToken?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};



$ b $ p
$ b

这是第二个返回服务器错误400的代码块(parse error) :

 函数insertRule(){

//获取日历ID,脚本用户的电子邮件以及用于访问Calendar API的API密钥
var calId ='abc123@group.calendar.google.com';
var userEmail = Session.getActiveUser()。getEmail();
var API_KEY ='012345abc123';
var newUserEmail ='person@gmail.com';

//获取访问Google Calendar API的授权
var apiName ='calendar';
var scope ='https://www.googleapis.com/auth/calendar';
var fetchArgs = googleOAuth_(apiName,scope);

//获取授权信息和给定日历
fetchArgs.method ='GET';

//创建POST请求体
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>;

//获取请求的内容(日历的ACL)
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);


解决方案

您正在使用 p>

  fetchArgs.method ='GET'; 

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

所以, 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:

So, it should be,

  fetchArgs.method = 'POST';

这篇关于使用Google Apps脚本创建POST正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 01:05
查看更多