Forum Discussion

harsh's avatar
harsh
Copper Contributor
Dec 10, 2024

Facing issue for Outlook calendar Sync with PHP

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.

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? 

No RepliesBe the first to reply

Resources