0

ユーザーの Google カレンダーに新しい所有者を追加する Google Apps スクリプトを作成しようとしています。以下の最初のコード ブロックは正しく機能します (カレンダー ACL を JSON 形式で返します)。Google Apps Script を使用して ACL に新しいユーザーを追加するにはどうすればよいですか? 2 番目のコード ブロックは、新しいルールを 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 (「解析エラー」) を返す 2 番目のコード ブロックを次に示します。

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

2 に答える 2

1

rawXML 文字列を作成するときに、この encodeURIComponent(newUserEmail) のように newUserEmail 変数をエンコードしてから再試行します。

于 2012-07-28T07:53:55.637 に答える
1

あなたが使用している:

  fetchArgs.method = 'GET';

しかし、ここにある Acl:insert ページには次のように書かれています。

HTTP リクエスト

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

だから、そうあるべきです、

  fetchArgs.method = 'POST';
于 2012-08-05T03:05:46.387 に答える