Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Api 如何从Google Apps脚本在Google日历ACL中插入规则_Api_Insert_Calendar_Google Apps Script_Acl - Fatal编程技术网

Api 如何从Google Apps脚本在Google日历ACL中插入规则

Api 如何从Google Apps脚本在Google日历ACL中插入规则,api,insert,calendar,google-apps-script,acl,Api,Insert,Calendar,Google Apps Script,Acl,如何将新用户添加到Google日历的ACL?我正在尝试发送一个POST HTTP请求。也许XML有问题?下面的代码生成一个服务器错误(400)。(编辑:显示oAuth) //--------------------------------------------------------------- //向“Fake Calendar 1.0”的访问控制列表添加规则 //---------------------------------------------------------------

如何将新用户添加到Google日历的ACL?我正在尝试发送一个POST HTTP请求。也许XML有问题?下面的代码生成一个服务器错误(400)。(编辑:显示oAuth)

//---------------------------------------------------------------
//向“Fake Calendar 1.0”的访问控制列表添加规则
//---------------------------------------------------------------
函数addRule(){
//获取日历ID、脚本用户的电子邮件和访问日历API的API密钥
var calId='12345calendar.google.com';
var userEmail=Session.getActiveUser().getEmail();
var API_键='ABC123';
var newUserEmail=person@example.net';
//获取访问Google日历API的授权
变量apiName=‘日历’;
var范围=https://www.googleapis.com/auth/calendar';
var fetchArgs=googleOAuth(apiName,范围);
fetchArgs.method='POST';
var rawXML=“”+
"" +
"" +
"" +
"";
fetchArgs.payload=rawXML;
fetchArgs.contentType='application/atom+xml';
//获取请求的内容(日历的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(内容);
}
//--------------------------------------------------------------
//谷歌OAuth
//--------------------------------------------------------------
googleOAuth函数(名称、范围){
var oAuthConfig=UrlFetchApp.addOAuthService(名称);
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(“匿名”);
oAuthConfig.setConsumerCret(“匿名”);
返回{oAuthServiceName:name,oAuthUseToken:“始终”};
}

在执行这段代码之前,您是否经历了oAuth授权过程。您的应用程序必须经过明确授权,才能使用日历API执行任何重要操作。Srik是正确的。您需要在UrlFetchApp中使用oAuth参数。 给定的参考URL显示了几个在应用程序脚本中使用oAuth与Google的RESTAPI一起工作的示例

优点:是的,我确实使用了oAuth参数。(代码示例更新)这与此问题非常相似:
//---------------------------------------------------------------
// Add a rule to the Access Control List for 'Fake Calendar 1.0'
//---------------------------------------------------------------
function addRule() {
  // Get Calendar ID, script user's email, and the API Key for access to Calendar API
  var calId = '12345calendar.google.com';
  var userEmail = Session.getActiveUser().getEmail();
  var API_KEY = 'ABC123';
  var newUserEmail = 'person@example.net';

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

  fetchArgs.method = '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:role value='owner'/>" +
               "<gAcl:scope type='user' value='"+userEmail+"'/>" +
               "</entry>";

  fetchArgs.payload = rawXML;
  fetchArgs.contentType = 'application/atom+xml';

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

//--------------------------------------------------------------
// Google OAuth 
//--------------------------------------------------------------
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"};
}