c# - 无法使用 Microsoft Graph API 创建事件

标签 c# asp.net-core microsoft-graph-api

我正在尝试创建一个 Event使用 The Microsoft Graph API在 .Net 5 中使用 ASP。

我去过 The Graph Explorer和它的声音一样奇怪,
有时但并非总是使用提供的演示代码创建事件(见下文)

{
    "subject": "My event 0",
    "start": {
        "dateTime": "2021-04-09T00:00:36.595Z",
        "timeZone": "UTC"
    },
    "end": {
        "dateTime": "2021-04-16T00:00:36.595Z",
        "timeZone": "UTC"
    }
}

"error": { "code": "RequestBodyRead", "message": "The property 'subject' does not exist on type 'Microsoft.OutlookServices.Event'. Make sure to only use property names that are defined by the type or mark the type as open type. REST APIs for this mailbox are currently in preview. You can find more information about the preview REST APIs at https://dev.outlook.com/." } }


这是一个 Event object 的负载我正在尝试创建一个事件。

{
      "allowNewTimeProposals": true,
      "attendees": [
        {
          "type": "required",
          "emailAddress": {
            "address": "andrew@andrew.com",
            "@odata.type": "microsoft.graph.emailAddress"
          },
          "@odata.type": "microsoft.graph.attendee"
        }
      ],
      "body": {
       "content": "hello,world",
       "contentType": "html",
       "@odata.type": "microsoft.graph.itemBody"
       },
      "end": {
      "dateTime": "2021-04-10T11:11:00.0000000",
      "timeZone": "AUS Eastern Standard Time",
      "@odata.type": "microsoft.graph.dateTimeTimeZone"
      },
      "isAllDay": false,
      "location": {
        "displayName": "Microsoft Teams Meeting",
        "@odata.type": "microsoft.graph.location"
       },
      "start": {
        "dateTime": "2021-04-10T11:10:00.0000000",
        "timeZone": "AUS Eastern Standard Time",
        "@odata.type": "microsoft.graph.dateTimeTimeZone"
      },
      "subject": "Andrew",
      "@odata.type": "microsoft.graph.event"
    }

Status Code: BadRequest Microsoft.Graph.ServiceException: Code: RequestBodyRead Message: A type named 'microsoft.graph.event' could not be resolved by the model. When a model is available, each type name must resolve to a valid type. REST APIs for this mailbox are currently in preview. You can find more information about the preview REST APIs at https://dev.outlook.com/.


可能值得一提的是,到目前为止,使用 SDK/API 时所有其他请求 GetPost 都已成功。

使用的范围

        public readonly static string[] Scopes =
        {
            "User.Read",
            "MailboxSettings.Read",
            "Calendars.ReadWrite",
        };

用于发送请求的代码。

        var timeZone = User.FindFirst("graph_timezone").Value;

        var @event = new Event()
        {
            Subject = teamsMeetingRequest.Title,
            Attendees = attendees,
            IsAllDay = teamsMeetingRequest.IsAllDay,
            Start = new DateTimeTimeZone
            {
                DateTime = teamsMeetingRequest.StartDate.ToString("o"),
                TimeZone = timeZone
            },
            End = new DateTimeTimeZone
            {
                DateTime = teamsMeetingRequest.EndDate.ToString("o"),
                TimeZone = timeZone
            },
            Body = new ItemBody()
            {
                Content = teamsMeetingRequest.Body,
                ContentType = BodyType.Html,
            },
            Location = new Location()
            {
                DisplayName = "Microsoft Teams Meeting"
            },
            AllowNewTimeProposals = true
        };

        await this._graphClient.Me.Events.Request().AddAsync(@event);

更新 StackTrace

Graph Explorer 和来自 SDK/API 的响应中的所有其他错误均在上面提到。

Status Code: BadRequest Microsoft.Graph.ServiceException:
Code: RequestBodyRead
Message: A type named 'microsoft.graph.event' could not be resolved 
  by the model. When a model is available, each type name must resolve 
  to a valid type. REST APIs for this mailbox are currently in preview. 
  You can find more information about the preview REST APIs at https://dev.outlook.com/.
Inner error:    
AdditionalData:     
date: 2021-04-10T02:46:11   
request-id: removed for question    
client-request-id: removed for question 
ClientRequestId: removed for question
   at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
   at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
   at TeamsMeetingCreator.Controllers.ValuesController.CreateTeamsMeeting(TeamsMeetingRequest teamsMeetingRequest) in Path To My File.


更新
我已经尝试使用 POSTMAN 发送带有上述负载的请求,所以不应该有任何 ODataType 问题,但不幸的是同样的错误存在。

"The property 'subject' does not exist on type 'Microsoft.OutlookServices.Event'

更新
我尝试创建一个没有属性的事件并且它已成功创建,然后我尝试更新事件

// Update The Event.
@event = await this._graphClient.Me.Events[emptyEvent.Id].Request().UpdateAsync(@event);

但是返回同样的错误

The property 'attendees' does not exist on type 'Microsoft.OutlookServices.Event

最佳答案

这可能是客户端库中的错误。在执行 AddAsync 之前尝试此操作:

@event.ODataType = null;

关于c# - 无法使用 Microsoft Graph API 创建事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67030052/

相关文章:

c# - 派生类导致 InsertOnSubmit() 抛出 NullReferenceException

amazon-web-services - Multi-Tenancy Web 应用程序上的 AWS Cognito 用户池

azure - 远程服务器返回错误: (405) The resource doesn't support specified Http Verb

angular - 无法将 Observable<Object> 类型转换为 Observable<CategoriesData> 类型

sharepoint - 如何使用文件夹路径和 Microsoft Graph API 1.0 上传文件

node.js - MS Graph API 和 DriveItem 搜索不适用于客户端凭据流

c# - .NET 4.5 的异步功能是否也适用于 MySql 和其他数据库?

c# - 区分 KeyboardHook 中的两个键盘

microsoft-graph-api - 如何在驱动器之间移动 OneDrive 中共享的文件

c# - Array.Exist() 只识别数组的最后一个元素