Create New Event using Outlook Api in PHP (using POST method)
Create New Event using Outlook Api in PHP (using POST method)
I am creating new event in php using POST method in Outlook API. I followed this link https://docs.microsoft.com/en-us/outlook/rest/php-tutorial to implement GET events method. Authorization code I am using is following
$oauthClient = new LeagueOAuth2ClientProviderGenericProvider([
'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 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"
]
' ;
$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);
the response of this call is
{ "error":
"code": "ErrorAccessDenied",
"message": "Access is denied. Check credentials and try again.",
"innerError":
"request-id": "7accd0e6-428c-4d71-....",
"date": "2018-09-06T11:18:29"
Microsoft app in dev center
Please help how can I resolve this issue
1 Answer
1
"message": "Access is denied. Check credentials and try again.",
Just as the message said, there are User authentication issue.
Use the console or any other way to output the Token. And check your redirectUri in dev center and project setting.
Consider efficiency, I suggest you follow the Microsoft Graph SDK for PHP to make all of our Event API Calls.
It is not a complete token: EwBwA8l6BAAURSN/FHlDW5xN74t6GzbtsBBeBUYAAX3kxuk8f5k4CIImOnBswbOmpsWXIprX2LvhYIiodysV60/Kpvorj3Ql3CRXT1dsNutFiPXXq74OFXpzlfQ7YipEZofOeYSYwyGNuYHXf9be38b5OKk5WI/jVTelQB6S5LnyyNDs3JCVbEOJtw2qxldWZ3toXDZZUnbg2RFLXMwkM3x2JUoiR68A/jM1/4gg+e+4NTLg0p9/O8AITnGIWmwBrRSDw0nwaZd+B7HRKOeRo6jcXSOx8QBOSkl+4Xn97eS/Petrl9PGCFrOdy+q1toPl3LsOFIZSNeOGfRGjS68MvfLDA1xl2JrOLdsm9/enSr6zKfPFCYXjL29BMUEi6YDZgAACLa+QUdCACvsQAIhK1Gw1ECO/ANSiewdl4m9FB8ArEsETI71DoDoJfQKi5hy93aaNydNwXe6yYf1CJZLMWllIwLB+CJ/qYQOwG.......
– Muhammad Ehtesham Anwar
Sep 11 '18 at 13:27
I have added dev center app screen shot please review it too.
– Muhammad Ehtesham Anwar
Sep 11 '18 at 14:00
Pls execute this line too:echo 'Auth URL: '.$oauthClient->getAuthorizationUrl(); I want to see the output ot it. And it should be better use localhost:8000/authorize as the redirectUri too. The one you use maybe cause some encoding issue.And add the following to your scope:openid profile offline_access User.Read
– Seiya Su
Sep 11 '18 at 14:48
this is the output. login.microsoftonline.com/common/oauth2/v2.0/…
– Muhammad Ehtesham Anwar
Sep 12 '18 at 8:00
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I am receiving token, redirectUri is "localhost/CodeIgniter-3.1.9/authorize" in app, and after the post method call it redirects to "localhost/CodeIgniter-3.1.9/…......" , is there any problem here? If you need token I can provide you with that to.
– Muhammad Ehtesham Anwar
Sep 11 '18 at 6:51