How to fix "Fatal error: Uncaught Microsoft\Graph\Generated\Models\ODataErrors\ODataError" issue?

Dev MDSK 0 Reputation points
2023-12-18T14:17:29.0666667+00:00
<?php

include 'vendor/autoload.php';

use Microsoft\Graph\Core\GraphClientFactory;
use Microsoft\Graph\GraphRequestAdapter;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\ApiException;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Graph\Generated\Users\Item\CalendarView\Item\EventItemRequestBuilderGetRequestConfiguration;
use Microsoft\Graph\Generated\Users\Item\UserItemRequestBuilderGetQueryParameters;
use Microsoft\Graph\Generated\Users\Item\UserItemRequestBuilderGetRequestConfiguration;
use Microsoft\Graph\Generated\Users\Item\Calendar\Events\EventsRequestBuilderGetRequestConfiguration;
use Microsoft\Graph\Generated\Users\Item\Calendars\CalendarsRequestBuilder;

echo '<pre>';
$tenantId = 'f8cdef31-a31e-4b4a-93e4-5f571e91255a';
$clientId = '981ecee8-1b95-4ce6-932d-f71ab583be34';
$clientSecret = 'zb-8Q~FB1TEFMAIv~tBNs_bPUq9HiEd6epylvcXk';
$scopes = ['calendars.Read', 'calendars.ReadBasic'];

$scopes = ['https://graph.microsoft.com/.default'];

$tokenRequestContext = new ClientCredentialContext(
        $tenantId,
        $clientId,
        $clientSecret
);

$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);

$result = $graphServiceClient->me()->calendars()->get()->wait();
print_r($result);

Microsoft Security | Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,376 Reputation points
    2023-12-19T06:49:14.9633333+00:00

    Hi @Dev MDSK

    You should use try-catch to capture your error details to help us troubleshoot for you.

    Judging from the script you provided, it should be an application-only context, so you won't be able to call the /me endpoint to get the logged in user's calendar, only the /users/{id} endpoint, as there is no user involved.

    Also, make sure you have granted the calendars.Read application permission to the calling app. Afterwards, remove the dynamic permissions in the script, which are not supported in the client credentials flow.

    $tenantId = 'f8cdef31-a31e-4b4a-93e4-5f571e91255a';
    $clientId = '981ecee8-1b95-4ce6-932d-f71ab583be34';
    $clientSecret = 'zb-8Q~FB1TEFMAIv~tBNs_bPUq9HiEd6epylvcXk';
    $scopes = ['https://graph.microsoft.com/.default'];
    
    $tokenRequestContext = new ClientCredentialContext(
            $tenantId,
            $clientId,
            $clientSecret
    );
    
    $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
    
    $result = $graphServiceClient->users()->byUserId('user-id')->calendars()->get()->wait();
    print_r($result);
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.