PHP code example of noardcode / laravel-microsoft-graph
1. Go to this page and download the library: Download noardcode/laravel-microsoft-graph library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
noardcode / laravel-microsoft-graph example snippets
namespace App\Http\Controllers;
use Noardcode\MicrosoftGraph\Requests\AuthorizationRequest;
use Noardcode\MicrosoftGraph\MicrosoftGraphClient;
use Noardcode\MicrosoftGraph\ValueObjects\AuthorizationResponse;
/**
* Example implementation of the noardcode/laravel-microsoft-graph package.
*
* Class Oauth2Controller
* @package App\Http\Controllers
*/
class Oauth2Controller extends Controller
{
/**
* @var MicrosoftGraphClient
*/
protected MicrosoftGraphClient $client;
/**
* This should be a unique/random string that is used to confirm the received request on the webhook
* belongs to the made authorization request. It must therefore be the same in both methods.
*
* @var string
*/
protected string $state = '1234567890';
/**
* Set the client.
*
* Oauth2Controller constructor.
* @param MicrosoftGraphClient $client
*/
public function __construct(MicrosoftGraphClient $client)
{
$this->client = $client;
}
/**
* Redirect the user to the Microsoft identity platform.
* A method like this could for example be called after clicking a button to connect with Microsoft 365.
*/
public function getUserConsent(): void
{
$this->client->authorize($this->state);
}
/**
* The webhook that is called by the Microsoft identity platform after the user has given his/her consent.
* The AuthorizationRequest class that is provided by the package immidiatly validates the response.
*
* With this response an access token is requested and returned.
* Save this access token (for example serialized in the databases users table) for later usage when you want to
* perform requests on the Graph API.
*
* @param MicrosoftGraphClient $client
* @param AuthorizationRequest $request
*/
public function getToken(MicrosoftGraphClient $client, AuthorizationRequest $request)
{
$accessToken = $client->requestAccessToken(
new AuthorizationResponse($request->all()),
$this->state
);
// Todo: Save the access token, for example by serializing the object (serialize($accessToken)).
}
}
$client = new MicrosoftGraphClient($accessToken);
$client = new MicrosoftGraphClient();
$client->setAccessToken($accessToken);
$client->getGraph();
$client->users()->get();
bash
php artisan vendor:publish --tag="config"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.