Create New Event using Outlook Api in PHP (using POST method) RRS feed

  • Question

  • I am creating new event in php using POST method in Outlook API. I followed official outlook rest api document to implement GET events method. Authorization code I am using is following

      $oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([
              'clientId'                => 'APP ID',
              'clientSecret'            => 'APP Password',
              'redirectUri'             => 'http://localhost/CodeIgniter-3.1.9/authorize',
              'urlAuthorize'            => '',
              'urlAccessToken'          => '',
              'urlResourceOwnerDetails' => '',
              'scopes'                  => ' Calendars.ReadWrite Calendars.ReadWrite.Shared Calendars.Read.Shared'
    to get token this is the code 
      $accessToken = $oauthClient->getAccessToken('authorization_code', ['code' => $_GET['code']]);

    Now I want to create Event from PHP app to outlook calendar. After Getting token, I have written following code to create new event with POST method. 

    $url = "";
                $data_json = '{
                    "subject": "Lets go for lunch",
                    "body": {
                      "contentType": "HTML",
                      "content": "Does late morning work for you?"
                    "start": {
                        "dateTime": "2017-04-15T12:00:00",
                        "timeZone": "Pacific Standard Time"
                    "end": {
                        "dateTime": "2017-04-15T14:00:00",
                        "timeZone": "Pacific Standard Time"
                        "displayName":"Harrys Bar"
                    "attendees": [
                        "emailAddress": {
                          "name": "Ehtesham Anwar"
                        "type": "required"
                  }' ;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                "Content-Type: application/json",
                "Authorization: Bearer " . $accessToken->getToken(),
                "Content-length: ".strlen($data_json))
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 $result = curl_exec($ch);
                 echo "<pre>";

    this is responding this error

      "error": {
        "code": "ErrorAccessDenied",
        "message": "Access is denied. Check credentials and try again.",
        "innerError": {
          "request-id": "7accd0e6-428c-4d71-a517-69d12b221cd9",
          "date": "2018-09-06T11:18:29"

    Anyone who can tell me what did I do wrong in this code. 

    Friday, September 7, 2018 6:39 AM