Garethp / php-ews

PHP Exchange Web Services
BSD 3-Clause "New" or "Revised" License
112 stars 45 forks source link

Connection to MailAPI works but connection to API results in UnauthorizedException #225

Closed basementmedia2 closed 2 years ago

basementmedia2 commented 2 years ago

Hi Gareth,

first of all: Great Fork / Work, Thanx for this amazing library.

My current problem is:

The following works:


use garethp\ews\MailAPI;
use garethp\ews\API\Type\IdFormatType;
use garethp\ews\API;
use garethp\ews\API\Enumeration\DistinguishedFolderIdNameType;
use garethp\ews\API\Enumeration;
use garethp\ews\API\Type;

$api = MailAPI::withUsernameAndPassword("outlook.office.com", myemail, mypassword);

But this

use garethp\ews\API;

 $api = API::withUsernameAndPassword("outlook.office.com", myemail, mypassword);
 $calendar = $api->getCalendar();

results in an Error message

Fatal error: Uncaught garethp\ews\API\Exception\UnauthorizedException in ..../vendor/garethp/php-ews/src/API/ExchangeWebServices.php:438 Stack trace: #0..../vendor/garethp/php-ews/src/API/ExchangeWebServices.php(356): garethp\ews\API\ExchangeWebServices->handleNonSuccessfulResponses(NULL, 401) #1 ..../vendor/garethp/php-ews/src/API/ExchangeWebServices/MiddlewareFactory.php(57): garethp\ews\API\ExchangeWebServices->processResponse(NULL) #2 .../vendor/garethp/php-ews/src/API/ExchangeWebServices.php(497): garethp\ews\API\ExchangeWebServices->garethp\ews\API\ExchangeWebServices\{closure}(Object(garethp\ews\API\MiddlewareRequest), Object(Closure)) #3 ..../vendor/garethp/php-ews/src/API/ExchangeWebServices/MiddlewareFactory.php(66): garethp\ews\API\ExchangeWebServices->garethp\ews\API\{closure}(Object(garethp\ews\API\MiddlewareRequest)) #4 ..../vendor/garethp/p in..../vendor/garethp/php-ews/src/API/ExchangeWebServices.php on line 438

Do you have an idea, why connecting to API does not work but connection to MailAPI with the same username/password does?

Best wishes Daniel

Garethp commented 2 years ago

Calling MailAPI::withUsernameAndPassword doesn't actually connect to the server by itself. It doesn't do anything until you try to make a call. If you want to test authentication, I suggest trying to call ->getServerTimezones(), it's a call that doesn't require any special permissions (as far as I know) as opposed to a few various things that might go wrong with getting a specific calendar. Can I get you to try the following:

var_dump(MailAPI::withUsernameAndPassword("outlook.office.com", myemail, mypassword)->getServerTimezones());
basementmedia2 commented 2 years ago

Hi Gareth,

the MailAPI works. I can read, send, delete Emails. But with the same username and password i am not able to connect to the calender. The following code results in the above posted error message:

require_once "vendor/autoload.php";
use garethp\ews\API;

$api = API::withUsernameAndPassword("outlook.office.com", myemail, mypassword);
$calendar = $api->getCalendar();

 //Get all items from midday today
 $items = $calendar->getCalendarItems('12:00 PM');

 //Get all items from 8 AM to 9 AM today
 $start = new DateTime('8:00 AM');
 $end = new DateTime('9:00 AM');

 $items = $calendar->getCalendarItems($start, $end);

 //Get a list of items in a Date Range
 $items = $calendar->getCalendarItems('01/12/2021', '31/12/2021');

Do you have further ideas? Or is the admin of the server the person to help? Is it a problem that we changed to outlook365 (the former server was owa....)?

Best wishes Daniel

Garethp commented 2 years ago

If you're using username/password authentication I can't think of anything about moving outlook365 that would change this, but it looks like a permission error on the server. I think it's definitely going to be something for your server admin to try and help with, sorry.

The only other way that I can think of for an error to occur would be if you're using OAuth authentication (shown here) which requires you to declare what permissions you want to request, but since you're using username/password authentication that shouldn't be an issue and there shouldn't be any differences between a call to the mail inbox vs. the call to a calendar

basementmedia2 commented 2 years ago

Ok, thank you for your help