Commit bcd1cc11 authored by root's avatar root

Initial commit

parents
<?php
namespace Nexus\MicrosoftGraphApi\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Microsoft\Graph\Exception\GraphException;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
class MicrosoftGraphController extends Controller
{
public function getOffice365Users()
{
$tenantId = config('app.office365_tenantId');
$clientId = config('app.office365_clientId');
$clientSecret = config('app.office365_clientSecret');
$guzzle = new Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $clientId,
'scope' => 'https://graph.microsoft.com/.default',
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
$headers = [
'Authorization' => 'Bearer ' . $token->access_token
];
//return dd($accessToken);
// Create a Graph client
$graph = new Graph();
$graph->setAccessToken($accessToken);
$users = $graph->createRequest('GET', '/users')
->setReturnType(Model\User::class)
->execute();
return dd($users);
}
public function getOffice365Groups()
{
$tenantId = config('app.office365_tenantId');
$clientId = config('app.office365_clientId');
$clientSecret = config('app.office365_clientSecret');
$guzzle = new Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $clientId,
'scope' => 'https://graph.microsoft.com/.default',
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
$headers = [
'Authorization' => 'Bearer ' . $token->access_token
];
//return dd($accessToken);
// Create a Graph client
$graph = new Graph();
$graph->setAccessToken($accessToken);
$groups = $graph->createRequest('GET', '/groups')
->setReturnType(Model\Group::class)
->execute();
return dd($groups);
}
}
<?php
namespace Nexus\MicrosoftGraphApi;
use Illuminate\Support\ServiceProvider;
class MicrosoftGraphServiceProvider extends ServiceProvider {
public function boot()
{
$this->loadRoutesFrom(__DIR__.'/routes/web.php');
}
public function register()
{
}
}
<?php
Route::group(['namespace' => 'Nexus\MicrosoftGraphApi\Http\Controllers', 'middleware' => ['web']], function(){
Route::get('users', 'MicrosoftGraphController@getOffice365Users');
Route::get('groups', 'MicrosoftGraphController@getOffice365Groups');
});
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment