How to fix issue for Outlook calendar Sync with PHP?

Harsh Prajapati 0 Reputation points
2024-12-17T08:09:57.0633333+00:00

When using Microsoft Graph Explorer, and making request, it’s returning the events data correctly.

  • But when in code we generate access token, it’s not able to make request and there is some permission issue,
  • So, the access token we are generating through code as per the official Microsoft documentation, isn’t having the required permission, even though we are specifying the required permission array as scope when making request for access token.
    Screenshot (87) new

Below is the code, I am using,
public function authorize() {

$auth_url = $this->Microsoft_calendar_model->createAuthUrl();

redirect($auth_url);

}

public function createAuthUrl() {

$authorize_url = "{$this->auth_base_url}/{$this->tenant_id}/oauth2/v2.0/authorize";

$queryParams = http_build_query([

'client_id' => $this->client_id,

'response_type' => 'code',

'redirect_uri' => $this->redirect_uri,

'scope' => implode(' ', $this->scopes),

'response_mode' => 'query',

'prompt' => 'consent'

]);

return "{$authorize_url}?{$queryParams}";

}

// User is getting prompt for permission consent and even though accepting all permissions isn't working

public function callback() {

$code = $this->input->get('code');  // I am getting the code here.

$access_token = $this->microsoft_calendar_model->fetchAccessTokenWithAuthCode($code);

}

// Issue occurs in the below function

public function fetchAccessTokenWithAuthCode($code) {

$token_url = "{$this->auth_base_url}/{$this->tenant_id}/oauth2/v2.0/token";

$client = new \GuzzleHttp\Client();

$response = $client->post($token_url, [

'headers' => [

'Content-Type' => 'application/x-www-form-urlencoded'

],

'form_params' => [

'client_id' => $this->client_id,

'client_secret' => $this->client_secret_value,

'redirect_uri' => $this->redirect_uri,

'code' => $code,

'grant_type' => 'authorization_code',

'scope' => 'offline_access Calendars.ReadWrite'

]

]);

$tokenData = json_decode($response->getBody()->getContents(), true);

$tokenContext = new AuthorizationCodeContext(

$this->tenant_id,

$this->client_id,

$this->client_secret_value,

$tokenData['access_token'],

$this->redirect_uri

);

$graphClient = new GraphServiceClient($tokenContext, $this->scopes);

$events = $graphClient->me()->calendars()->byCalendarId('primary')->events()->get()->wait();

pre($events);

}
What I'm doing wrong, I can't figure it out anywhere?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,956 questions
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 45,021 Reputation points
    2024-12-17T09:17:34.1033333+00:00

    Hi @Harsh Prajapati

    Using guest user to get/create events is not supported because guest user does not have an EXO mailbox available in the current tenant, it can only get/create events as its identity in the home tenant.

    Try configuring the calling app as a multi-tenant app, then change /{tenant_id} to /common and log in.

    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.