1

Google カレンダーの ACL に新しいユーザーを追加するにはどうすればよいですか? POST HTTP リクエストを送信しようとしています。おそらくXMLに何か問題がありますか?以下のコードは、サーバー エラー (400) を生成します。(編集: 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"};
}
4

2 に答える 2

0

スリックは正しい。UrlFetchAppでoAuth引数を使用する必要があります。指定された参照URLは、AppsスクリプトでoAuthを使用してGoogleのRESTAPIを操作するためのいくつかの例を示しています https://sites.google.com/site/appsscripttutorial/urlfetch-and-oauth

于 2012-07-27T05:57:44.197 に答える
0

このコードを実行する前に、oAuth 承認プロセスを完了しましたか。Calendar API で重要なことを実行するには、アプリを明示的に承認する必要があります。

于 2012-07-27T03:11:05.037 に答える