使用Microsoft Graph、Laravel创建会议时start字段Schema不匹配的400错误排查
问题:使用Microsoft Graph创建事件时出现BadRequest错误(start字段不匹配schema)
错误信息
Client error:
POST https://graph.microsoft.com/v1.0/me/calendar/eventsresulted in a400 Bad Request
response:{"error":{"code":"BadRequest","message":"Property start in payload has a value that does not match schema.","innerError":{"date":"2023-02-01T09:57:57","request-id":"3430668e-7bb5-46e1-8960-904f1e2d1521","client-request-id":"3430668e-7bb5-46e1-8960-904f1e2d1521"}}}
当前实现代码
public function createMicrosoftMeeting($data) { $access_token = Cookie::get('access_tokenz'); $graph = new Graph(); $graph->setAccessToken($access_token); $start_time ='2023-04-15T12:00:00'; $end_time = '2023-04-15T12:50:00'; $event = new Event(); $event->setSubject($data['title']['en']); $event->setBody(new ItemBody([ 'contentType' => ItemBody::CONTENT_TYPE_HTML, 'content' => strip_tags($data['description']) ])); $event->setStart($start_time); $event->setEnd($end_time); try { $calendar = $graph->createRequest("POST", "/me/calendar/events") ->attachBody($event) ->execute(); return $calendar->getOnlineMeetingUrl(); } catch (\Exception $e) { return $e->getMessage(); } }
事件实例及Payload
Microsoft\Graph\Model\Event {#2155 #_propDict: array:4 [ "subject" => "sample test " "body" => Microsoft\Graph\Model\ItemBody {#2139 #_propDict: array:2 [ "contentType" => "html" "content" => "In the instance is throws an error, check if the server space is full df -hIf there is no space, clear out files that are no longer need in the /var/www/html directoryOnce done, check the server space again " ] } "start" => "2023-04-15T12:00:00" "end" => "2023-04-15T12:50:00" ] }
问题原因和解决方法
原因
Microsoft Graph的Event模型要求start和end字段必须是包含dateTime和timeZone的对象结构,直接传入字符串不符合API的schema定义,导致触发BadRequest错误。
解决方法
需要使用DateTimeTimeZone对象来封装时间信息,而非直接传入字符串:
- 先导入
DateTimeTimeZone类(未导入时需添加):
use Microsoft\Graph\Model\DateTimeTimeZone;
- 修改时间字段的设置逻辑:
// 用DateTimeTimeZone对象包装时间和时区 $start_time = new DateTimeTimeZone([ 'dateTime' => '2023-04-15T12:00:00', 'timeZone' => 'UTC' // 可根据实际需求替换为对应时区,如'Asia/Shanghai' ]); $end_time = new DateTimeTimeZone([ 'dateTime' => '2023-04-15T12:50:00', 'timeZone' => 'UTC' ]); $event->setStart($start_time); $event->setEnd($end_time);
修改后的完整代码
use Microsoft\Graph\Model\DateTimeTimeZone; public function createMicrosoftMeeting($data) { $access_token = Cookie::get('access_tokenz'); $graph = new Graph(); $graph->setAccessToken($access_token); // 改用DateTimeTimeZone对象设置时间 $start_time = new DateTimeTimeZone([ 'dateTime' => '2023-04-15T12:00:00', 'timeZone' => 'UTC' // 根据实际场景替换时区 ]); $end_time = new DateTimeTimeZone([ 'dateTime' => '2023-04-15T12:50:00', 'timeZone' => 'UTC' ]); $event = new Event(); $event->setSubject($data['title']['en']); $event->setBody(new ItemBody([ 'contentType' => ItemBody::CONTENT_TYPE_HTML, 'content' => strip_tags($data['description']) ])); $event->setStart($start_time); $event->setEnd($end_time); try { $calendar = $graph->createRequest("POST", "/me/calendar/events") ->attachBody($event) ->execute(); return $calendar->getOnlineMeetingUrl(); } catch (\Exception $e) { return $e->getMessage(); } }
内容的提问来源于stack exchange,提问作者mnmlst
相关产品推荐
相关产品推荐

