Created
March 25, 2022 14:50
-
-
Save Dillonsmart/36a07cbbdc3bcfad99d0b27a19131d62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Integrations; | |
use Microsoft\Graph\Exception\GraphException; | |
use Microsoft\Graph\Graph; | |
use Microsoft\Graph\Model; | |
trait MicrosoftGraph | |
{ | |
public static function connect(): Graph | |
{ | |
$guzzle = new \GuzzleHttp\Client(); | |
$url = 'https://login.microsoftonline.com/' . env('MS_TENANT_ID') . '/oauth2/token?api-version=' . env('MS_GRAPH_API_VERSION'); | |
$response = json_decode($guzzle->post($url, [ | |
'form_params' => [ | |
'client_id' => env('MS_CLIENT_ID'), | |
'client_secret' => env('MS_CLIENT_SECRET'), | |
'resource' => 'https://graph.microsoft.com/', | |
'grant_type' => 'client_credentials', | |
], | |
])->getBody()->getContents()); | |
$graph = new Graph(); | |
return $graph->setBaseUrl("https://graph.microsoft.com") | |
->setApiVersion(env('MS_GRAPH_API_VERSION')) | |
->setAccessToken($response->access_token); | |
} | |
/** | |
* Get all the users in the tenant | |
* | |
* @return mixed | |
* @throws GraphException | |
*/ | |
public static function users() | |
{ | |
$graph = self::connect(); | |
$query = '/users'; | |
return $graph->createRequest("get", $query) | |
->addHeaders(array("Content-Type" => "application/json")) | |
->setReturnType(Model\User::class) | |
->setTimeout("1000") | |
->execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment