London's avatar
Level 3

Microsoft Graph

I am looking for an example of Microsoft Graph 2.0 and Laravel 12. I can not find one. The example that is from Microsoft uses Laravel 11.

0 likes
6 replies
LaryAI's avatar
Level 58

To integrate Microsoft Graph 2.0 with Laravel 12, you can follow these steps. This example assumes you want to authenticate users via Microsoft and access their data using Microsoft Graph.

Step 1: Install Required Packages

First, ensure you have the necessary packages installed. You can use the microsoft/microsoft-graph package for interacting with Microsoft Graph and league/oauth2-client for OAuth2 authentication.

composer require microsoft/microsoft-graph
composer require league/oauth2-client

Step 2: Set Up Environment Variables

Add the necessary Microsoft Graph credentials to your .env file:

MICROSOFT_CLIENT_ID=your-client-id
MICROSOFT_CLIENT_SECRET=your-client-secret
MICROSOFT_REDIRECT_URI=https://your-app-url.com/callback

Step 3: Create an OAuth2 Provider

Create a custom OAuth2 provider for Microsoft. You can place this in app/Providers/MicrosoftProvider.php.

Step 4: Implement Authentication Logic

Create routes and controllers to handle the authentication flow.

In routes/web.php:

Step 5: Access Microsoft Graph

Once authenticated, you can use the microsoft/microsoft-graph package to interact with Microsoft Graph.

use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;

$graph = new Graph();
$graph->setAccessToken($accessToken->getToken());

$user = $graph->createRequest("GET", "/me")
              ->setReturnType(Model\User::class)
              ->execute();

dd($user);

This setup will allow you to authenticate users with Microsoft and access their data using Microsoft Graph in a Laravel 12 application. Adjust the scopes and endpoints as needed for your specific use case.

1 like
vincent15000's avatar

You should replace env('MICROSOFT_CLIENT_ID') by config('microsoft_client_id') and add this key in a configuration file.

London's avatar
Level 3

I get this error. Method 'App\Providers\MicrosoftProvider::getResourceOwnerDetailsUrl()' is not compatible with method 'League\OAuth2\Client\Provider\AbstractProvider::getResourceOwnerDetailsUrl()'.

1 like
vincent15000's avatar

@London In which context do you get this error ? From which class ? Can you show the code ?

Please or to participate in this conversation.