[英]Missing End Time - Google Calendar API call to insert event
请帮助解释为什么下面的代码在尝试代表我们域中的另一个用户(使用委托)插入事件时返回错误,指出结束日期时间丢失...
我可以从他们的日历中获取数据,而不是发布事件。
我已经尝试过在请求中使用和不使用 calendarId 和资源,以及在开始/结束对象中使用和不使用时区。 我搜索过的其他所有内容都是空白。
function testRequest(){
var service = getDWDS('Calendar: ', 'https://www.googleapis.com/auth/calendar', 'user@example.com');
var requestBody =
{
calendarId: "user@example.com",
resource: {
start: {
dateTime: '2022-12-09T09:00:00Z',
},
end: {
dateTime: '2022-12-09T11:30:00Z',
},
summary: "Test Request",
conferenceData: {
createRequest: {
requestId: 'random'
}
},
}
};
requestBody.headers = {'Authorization': 'Bearer ' + service.getAccessToken()};
requestBody.contentType = "application/json";
requestBody.method = "POST";
requestBody.muteHttpExceptions = false;
var url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events?conferenceDataVersion=1&sendUpdates=none';
Logger.log(requestBody)
Logger.log(JSON.parse(UrlFetchApp.fetch(url, requestBody)))
return JSON.parse(UrlFetchApp.fetch(url, requestBody));
}
const OAUTH2_SERVICE_ACCOUNT_PRIVATE_KEY = "KEY GOES HERE"
const OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL = 'SERVICE ACCOUNT EMAIL HERE';
function getDWDS(serviceName, scope, email) {
// Logger.log('starting getDomainWideDelegationService for email: ' + email);
return OAuth2.createService(serviceName + email)
// Set the endpoint URL.
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the private key and issuer.
.setPrivateKey(OAUTH2_SERVICE_ACCOUNT_PRIVATE_KEY)
.setIssuer(OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL)
// Set the name of the user to impersonate. This will only work for
// Google Apps for Work/EDU accounts whose admin has setup domain-wide
// delegation:
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
.setSubject(email)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scope. This must match one of the scopes configured during the
// setup of domain-wide delegation.
.setScope(scope);
}
谢谢
在你的脚本中,我认为请求日历 API 的脚本需要修改。 并且,在您的脚本中,完成了 2 个请求。 那么,下面的修改呢?
在这个修改中,你的testRequest()
被修改了。
function testRequest() {
var service = getDWDS('Calendar: ', 'https://www.googleapis.com/auth/calendar', 'user@example.com');
var requestBody = {
method: "POST",
headers: { 'Authorization': 'Bearer ' + service.getAccessToken() },
contentType: "application/json",
payload: JSON.stringify({
start: { dateTime: '2022-12-09T09:00:00Z', },
end: { dateTime: '2022-12-09T11:30:00Z', },
summary: "Test Request",
conferenceData: { createRequest: { requestId: 'random' } }
}),
};
var url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events?conferenceDataVersion=1&sendUpdates=none';
var res = UrlFetchApp.fetch(url, requestBody);
return JSON.parse(res.getContentText());
}
service.getAccessToken()
访问令牌是用于请求'https://www.googleapis.com/calendar/v3/calendars/primary/events
”端点的有效访问令牌。 请注意这一点。 当与授权和权限相关的错误时,请再次确认您的设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.