我的 Microsoft 365 组织中有多个用户具有相应的电子邮件地址。
我希望能够以组织管理员的身份安排用户之间的会议,而不是作为用户之一。
我尝试使用的每种身份验证方法
ms graph api
都会导致错误,指出日历安排仅在委托模式下可用。
不确定这里的正确方法是什么,我需要一种完全编程的方式来控制我的组织用户的日历。 要求用户使用 oauth2 进行身份验证(需要用户交互)的解决方案不适合这种情况。
这里是代码,目前有证书认证。
const tenantId = '###';
const clientId = '###';
const certificatePath = 'file.pem';
const credential = new ClientCertificateCredential(
tenantId,
clientId,
certificatePath,
);
// @microsoft/microsoft-graph-client/authProviders/azureTokenCredentials
const authProvider = new TokenCredentialAuthenticationProvider(credential, {
scopes: ['https://graph.microsoft.com/.default'],
});
const graphClient = Client.initWithMiddleware({ authProvider: authProvider });
const event = {
subject: 'Testing meeting scheduling',
body: {
contentType: 'HTML',
content: 'Is it working?',
},
start: {
dateTime: '2024-05-22T11:00:00',
timeZone: 'Pacific Standard Time',
},
end: {
dateTime: '2024-05-22T12:00:00',
timeZone: 'Pacific Standard Time',
},
location: {
displayName: 'Room 22',
},
attendees: [
{
emailAddress: {
address: '[email protected]',
name: 'User A',
},
type: 'required',
},
{
emailAddress: {
address: '[email protected]',
name: 'User B',
},
type: 'required',
},
],
transactionId: '?',
};
const calendarId = '################';
console.log("Scheduling....")
graphClient.api(`/me/calendars/${calendarId}/events`).post(event)
.then(response => {
console.log('Event created successfully:', response);
})
.catch(error => {
console.error('Error creating event:', error);
});
出现错误:
{
"code": "BadRequest",
"message": "/me request is only valid with delegated authentication flow.",
"innerError": {
"date": "2024-04-30T09:04:28",
"request-id": "921c1689-14a7-4452-8870",
"client-request-id": "2dd01605-5566-0f41"
}
}
如果您使用通过 客户端凭证
流生成的令牌调用
/me
端点,通常会发生错误。
我注册了一个 Entra ID 应用程序并添加了 Application
类型的
Calendars.ReadWrite
权限,如下所示:
当我使用客户端凭据流生成的令牌调用
/me
端点时,我也遇到了相同的错误,如下所示:
const calendarId = '################';
console.log("Scheduling....")
graphClient.api(`/me/calendars/${calendarId}/events`).post(event)
.then(response => {
console.log('Event created successfully:', response);
})
.catch(error => {
console.error('Error creating event:', error);
});
回复:
要解决该错误,请将代码中的
/me
替换为 "/users/userId",因为客户端凭证流不是支持 /me
端点的 delegate流。
当我用
/users/userId
替换代码后再次运行代码时,我得到了 response ,其中事件创建成功,如下所示:
const calendarId = '################';
console.log("Scheduling....")
graphClient.api(`/users/userId/calendars/${calendarId}/events`).post(event)
.then(response => {
console.log('Event created successfully:', response);
})
.catch(error => {
console.error('Error creating event:', error);
});
回复: