I have installed and executed the basic example for MS Graph API through the PHP SDK. The examples contain a class named GraphHelper, which has a method getInbox:
public static function getInbox(): ModelsMessageCollectionResponse {
$configuration = new MessagesRequestBuilderGetRequestConfiguration();
$configuration->queryParameters = new MessagesRequestBuilderGetQueryParameters();
// Only request specific properties
$configuration->queryParameters->select = ['from','isRead','receivedDateTime','subject'];
$configuration->queryParameters->filter = "(from/emailAddress/address) eq '[email protected]'";
// Sort by received time, newest first
$configuration->queryParameters->orderby = ['receivedDateTime DESC'];
// Get at most 25 results
$configuration->queryParameters->top = 25;
return GraphHelper::$userClient->me()
->mailFolders()
->byMailFolderId('inbox')
->messages()
->get($configuration)->wait();
}
If I omit the line $configuration->queryParameters->filter = "(from/emailAddress/address) eq '[email protected]'";
, I get the 25 latest messages from my inbox. When I apply filter, no matter what filter I put, I get the following:
Error getting user's inbox:
MicrosoftGraphGeneratedModelsODataErrorsODataError in /home/alexios/research/automation/finances/automation-finances-import/vendor/microsoft/microsoft-graph/src/Generated/Models/ODataErrors/ODataError.php:36
Stack trace:
#0 /home/alexios/research/automation/finances/automation-finances-import/vendor/microsoft/kiota-serialization-json/src/JsonParseNode.php(117): MicrosoftGraphGeneratedModelsODataErrorsODataError::createFromDiscriminatorValue()
#1 /home/alexios/research/automation/finances/automation-finances-import/vendor/microsoft/kiota-http-guzzle/src/GuzzleRequestAdapter.php(678): MicrosoftKiotaSerializationJsonJsonParseNode->getObjectValue()
#2 /home/alexios/research/automation/finances/automation-finances-import/vendor/microsoft/kiota-http-guzzle/src/GuzzleRequestAdapter.php(160): MicrosoftKiotaHttpGuzzleRequestAdapter->throwFailedResponse()
#3 /home/alexios/research/automation/finances/automation-finances-import/vendor/php-http/promise/src/FulfilledPromise.php(39): MicrosoftKiotaHttpGuzzleRequestAdapter->MicrosoftKiotaHttp{closure}()
#4 /home/alexios/research/automation/finances/automation-finances-import/vendor/microsoft/kiota-http-guzzle/src/GuzzleRequestAdapter.php(145): HttpPromiseFulfilledPromise->then()
#5 /home/alexios/research/automation/finances/automation-finances-import/vendor/microsoft/microsoft-graph/src/Generated/Users/Item/MailFolders/Item/Messages/MessagesRequestBuilder.php(73): MicrosoftKiotaHttpGuzzleRequestAdapter->sendAsync()
#6 /home/alexios/research/automation/finances/automation-finances-import/src/Microsoft/GraphHelper.php(80): MicrosoftGraphGeneratedUsersItemMailFoldersItemMessagesMessagesRequestBuilder->get()
#7 /home/alexios/research/automation/finances/automation-finances-import/bin/import.php(25): AlexiosTsiaparasAutomationFinancesImportMicrosoftGraphHelper::getInbox()
#8 /home/alexios/research/automation/finances/automation-finances-import/bin/import.php(85): listInbox()
#9 {main}
So, no error description whatsoever.
I got the filtering from https://learn.microsoft.com/en-us/graph/use-the-api#query-parameters, and checked out the Graph Explorer https://developer.microsoft.com/en-us/graph/graph-explorer.
The get my mails from an address example has the filter I am interested in:
I tried url encoding the filter, and not, the error remains the same.
Update
I tried to bypass the SDK, except for the access token, so I did the following:
$accessToken = GraphHelper::getUserToken();
// echo "Token: $tokenn";
$params = [
'select' => ['from','isRead','receivedDateTime','subject'],
'orderby' => ['receivedDateTime DESC'],
'top' => 3,
'filter' => "(from/emailAddress/address) eq '[email protected]'"
];
$ch = curl_init();
$url = 'https://graph.microsoft.com/v1.0/me/messages?'.http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $accessToken));
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
echo "HTTP status: $httpcodenError:n";
print_r($err);
echo "nOutputn------n";
echo $output;
Everything works and I manage to get a response back with expected results!