2012-08-16 34 views
2

我正在嘗試創建一個Web應用程序,該應用程序允許用戶將自己作爲所有者添加到日曆中。我試圖在日曆的ACL中插入新規則,但我無法獲得使用日曆API訪問的權限。無法對服務進行身份驗證:日曆

當我授權腳本運行彈出的對話框後,我收到以下錯誤消息:「無法對服務進行身份驗證:日曆。」

我在做什麼不正確?

function insertRule() { 
    ScriptApp.invalidateAuth(); 
    var calId = MY_CAL_ID; 
    var userEmail = Session.getActiveUser().getEmail(); 
    var API_KEY = MY_API_KEY; 

    var apiName = 'calendar'; 
    var scope = 'https://www.googleapis.com/auth/calendar'; 
    var fetchArgs = googleOAuth_(apiName, scope); 

    fetchArgs.method = "POST"; 

    var payload = 
    { 
     "role" : "owner", 
     "scope" : 
     { 
     "type" : "user", 
     "value" : userEmail 
     } 
    }; 

    fetchArgs.payload = payload; 

    var base = 'https://www.googleapis.com/calendar/v3/calendars/'; 
    var url = base + calId + '/acl?key=' + API_KEY; 

    var response = UrlFetchApp.fetch(url, fetchArgs).getContentText(); 

}

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"}; 
} 

回答