Search code examples
eventsgraphcalendarcomposer-phpmicrosoft-graph-api

Error creating events with Microsoft graph


$p=array(
        'subject'=>'Registrado desde iSchool',
        'body'=>array(
                    'contentType'=>'HTML',
                    'content'=>'Evento de prueba',                
                ),
        'start'=>array(
                    'dateTime'=>'2017-05-28T12:00:00',
                    'timeZone'=>'Pacific Standard Time'
                ),
        'end'=>array(
                    'dateTime'=>'2017-05-28T17:00:00',
                    'timeZone'=>'Pacific Standard Time'
                ),
        'location'=>array('displayName'=>'Mi casa'),
        'attendees'=>array(
                        'emailAddress'=>array('address'=>'email', 'name'=>'name'),
                        'type'=>'required'
                    ),            
    );
    $this->crear('calendars/'.$this->mg_model->idCalendarioUsuario().'/events', $p); 

This function "$this->mg_model->idCalendarioUsuario()" Return the calendar ID

public function crear($objeto, $datos){
    //$this->graph->setApiVersion("beta");
    $r = $this->graph->createRequest("POST", "/me/$objeto")      
        //->addHeaders(array("Content-Type" => "application/json"))    
        ->attachBody($datos)
        ->setReturnType(Event::class)    
        ->execute();
} 

Error: 400 Bad Request` response: { "error": { "code": "BadRequest", "message": "Property attendees in payload has a value that does not matc (truncated...)     

What am I doing wrong?


Solution

  • The JSON payload for your example should look like this:

    {
        "subject": "Registrado desde iSchool",
        "body": {
            "contentType": "HTML",
            "content": "Evento de prueba"
        },
        "start": {
            "dateTime": "2017-05-28T12:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "end": {
            "dateTime": "2017-05-28T17:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "location": {
            "displayName": "Mi casa"
        },
        "attendees": [{
            "emailAddress": {
                "address": "email",
                "name": "name"
            },
            "type": "required"
        }]
    }
    

    Your attendees collection however is rendering as an object instead of an array. It is rendering your attendees as an object instead of an array. This is likely the cause of that payload error.

    "attendees": {
        "emailAddress": {
            "address": "email",
            "name": "name"
        },
        "type": "required"
    }
    

    Please note, I'm not a PHP expert so my code here may be rather inelegant. That said, I believe you can ensure it renders as an array by wrapping it in an additional array():

    'attendees'=>array(array(
        'emailAddress'=>array('address'=>'email', 'name'=>'name'),
        'type'=>'required'
    )),