php-ews icon indicating copy to clipboard operation
php-ews copied to clipboard

UnauthorizedException in reading emails using oauth2

Open laurelBE opened this issue 7 years ago • 1 comments

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

laurelBE avatar May 11 '18 09:05 laurelBE

I have the same problem, how do you fix it?

fglueck avatar Oct 27 '20 13:10 fglueck