UnauthorizedException in reading emails using oauth2
Hi , I am trying to load email list using oauth 2, Following is my code
<?php
require_once "vendor/autoload.php";
use src\API\Type;
use garethp\ews\MailAPI as API;
$token='EwA......'; //access token
$api = API::withCallbackToken('outlook.office365.com', $token);
$mail = $api->getMailItems();
foreach ($mail as $email) {
$email = $api->getItem($email->getItemId());
$email_id=$email->getItemId();
$subject = $email->getSubject();
$sender = $email->getSender()->getMailbox()->getEmailAddress();
$body = (string) $email->getBody();
// Push into object
array_push($data, array("sender" => $sender, "subject" => $subject, "body" => $body, "buttons" => $buttons));
}
echo json_encode($data);
but getting response as follows:
Fatal error: Uncaught garethp\ews\API\Exception\UnauthorizedException in C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php:438 Stack trace: #0 C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php(356): garethp\ews\API\ExchangeWebServices->handleNonSuccessfulResponses(NULL, 401) #1 C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices\MiddlewareFactory.php(57): garethp\ews\API\ExchangeWebServices->processResponse(NULL) #2 C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php(497): garethp\ews\API\ExchangeWebServices->garethp\ews\API\ExchangeWebServices\{closure}(Object(garethp\ews\API\MiddlewareRequest), Object(Closure)) #3 C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices\MiddlewareFactory.php(66): garethp\ews\API\ExchangeWebServices->garethp\ews\API\{closure}(Object(garethp\ews\API\MiddlewareRequest)) #4 C:\inetpub\wwwroot\bephpforTt in C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php on line 438
Please help me
I have the same problem, how do you fix it?