2012-07-27 77 views
0

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

function getCalendarACL() { 

    // Get Calendar ID, script user's email, and the API Key for access to Calendar API 
    var calId = '[email protected]'; 
    var userEmail = Session.getActiveUser().getEmail(); 
    var API_KEY = 'abc123'; 

    // 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的第二码块(“分析错误”):

function insertRule() { 

    // Get Calendar ID, script user's email, and the API Key for access to Calendar API 
    var calId = '[email protected]'; 
    var userEmail = Session.getActiveUser().getEmail(); 
    var API_KEY = 'abc123'; 
    var newUserEmail = '[email protected]'; 

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

你是怎么用这个@KarBytes去的?下面答案中的电子邮件变量编码是否解决了添加用户的问题,或者您是否设法以其他方式解决问题? – 2012-08-02 03:24:46

+0

编码电子邮件变量没有改变任何东西。我认为rawXML可能需要标题。当我使用API​​的第2版时,我收到服务器错误404:'var base ='https://www.googleapis.com/calendar/v2/calendars/';' – KarBytes 2012-08-03 15:56:18

回答

1

我将编码这样encodeURIComponent方法(newUserEmail)使rawXML字符串,然后当newUserEmail可变重试。

1

您正在使用:

但在最小ACL:插入页面发现here这样说:

HTTP请求

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

左右,应该是,

fetchArgs.method = 'POST';