我正在嘗試創建一個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);
}
你是怎麼用這個@KarBytes去的?下面答案中的電子郵件變量編碼是否解決了添加用戶的問題,或者您是否設法以其他方式解決問題? – 2012-08-02 03:24:46
編碼電子郵件變量沒有改變任何東西。我認爲rawXML可能需要標題。當我使用API的第2版時,我收到服務器錯誤404:'var base ='https://www.googleapis.com/calendar/v2/calendars/';' – KarBytes 2012-08-03 15:56:18