我们可以在具有应用程序权限的用户日历中创建事件吗?

问题描述 投票:0回答:1

我正在开发一个项目,需要在用户的 Outlook 日历中创建事件。要求是将工作检查日期添加到相关用户的日历中。此外,用户应该能够在其日历上手动创建事件。

经过一些研究,我确定了两种可能的方法来实现此功能。鉴于这两种方法,我不确定哪一种适合我的要求。另外,我对这两个问题都有疑问:

1。委托权限:

通过这种方法,我将利用委托身份验证流程,要求使用用户的访问令牌在其日历中创建事件。

但是,我注意到这些代币的过期时间相对较短,通常不到一天。管理所有用户的代币似乎不切实际。

所以,我意识到我们可能需要应用程序权限。

2。应用权限:

仅使用应用程序的访问令牌来管理它是可行的。

我有此端点的应用程序访问令牌。

https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token

但是当我尝试使用 https://graph.microsoft.com/v1.0/users/${userId}/events

创建事件时

我收到如下错误:

response: {
  status: 401,
  statusText: 'Unauthorized',
  data: {
    error: {
      code: 'OrganizationFromTenantGuidNotFound',
      message: "The tenant for tenant guid 'f8cdef31-xxxxx-5f571e91255a' does not exist.",
      innerError: {
        oAuthEventOperationId: 'e3f0be2e-xxxxx-a3cabb87741d',
        oAuthEventcV: 'CNUKxxoGwp3Jr9+kMI/pZw.1.1.1',
        errorUrl: 'https://aka.ms/autherrors#error-InvalidTenant',
        requestId: '446a24c5-xxxxx-f592fcef453c',
        date: '2024-03-15T05:16:08',
      },
    },
  },
}

我不确定我做错了什么。我愿意接受任何见解、建议或澄清。谢谢您的协助。

azure outlook azure-active-directory microsoft-graph-api azure-ad-graph-api
1个回答
0
投票

我们可以在具有应用程序权限的用户日历中创建事件吗?

是的,您可以在应用程序许可的情况下在用户的日历中创建事件。

创建 Microsoft Entra 应用程序并授予

Calendars.ReadWrite
应用程序权限:

enter image description here

通过邮差生成访问令牌

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
client_secret:ClientSecret
grant_type:client_credentials
scope:https://graph.microsoft.com/.default

enter image description here

通过使用上述访问令牌,我能够成功为用户创建日历事件

POST https://graph.microsoft.com/v1.0/users/UserID/calendars/CalendarID/events

Content-type: application/json

{
  "subject": "Let's go for lunch",
  "body": {
    "contentType": "HTML",
    "content": "Does mid month work for you?"
  },
  "start": {
      "dateTime": "2024-03-15T12:00:00",
      "timeZone": "Pacific Standard Time"
  },
  "end": {
      "dateTime": "2024-03-15T14:00:00",
      "timeZone": "Pacific Standard Time"
  },
  "location":{
      "displayName":"Harry's Bar"
  },
  "attendees": [
    {
      "emailAddress": {
        "address":"xxx.com",
        "name": "xxx"
      },
      "type": "required"
    }
  ],
  "transactionId":"xxx"
}

enter image description here

如果您尝试为 Microsoft 个人帐户创建事件并利用客户端凭据流生成令牌并调用 users/UserID/calendars/CalendarID/events 端点,通常会出现错误

“OrganizationFromTenantGuidNotFound”

  • 如果您尝试为 Microsoft 个人帐户创建事件,则需要使用委托流来生成令牌并调用
    /me/calendars/CalendarId/events
    端点。请参考 SrideviSO 主题
  • 如果您正在呼叫其他租户用户,请调用
  • organizations/common 端点来生成令牌。
  • 如果问题仍然存在,请确保用户拥有 Office 365 许可证。

要增加访问令牌的生命周期,您可以参考我的这个SO线程

参考:

创建事件-Microsoft Graph v1.0 |微软

© www.soinside.com 2019 - 2024. All rights reserved.