我正在尝试创建一个 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"};
}