none
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'            => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
              'urlAccessToken'          => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
              '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 = "https://graph.microsoft.com/beta/me/events";
                $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"
                    },
                    "location":{
                        "displayName":"Harrys Bar"
                    },
                    "attendees": [
                      {
                        "emailAddress": {
                          "address":"ehteshamanwar_86@hotmail.com",
                          "name": "Ehtesham Anwar"
                        },
                        "type": "required"
                      }
                    ]
                  }' ;
                 //print_r($data_json);die;
                $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>";
                 print_r($result);

    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