Commit c3132a0a authored by Mark Siy's avatar Mark Siy

updates on office365 api

parent 361c6306
......@@ -11,5 +11,10 @@
"guzzlehttp/guzzle": "^6.5",
"league/oauth2-client": "dev-master",
"microsoft/microsoft-graph": "^1.12"
},
"autoload": {
"psr-4": {
"Nexus\\MicrosoftGraphApi\\": "src/"
}
}
}
<?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');
});
?>
<?php
namespace Nexus\MicrosoftGraphApi\Http\Controllers;
use App\Http\Controllers\Controller;
namespace Nexus\MicrosoftGraphApi;
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
class Api
{
public function getOffice365Users()
public static function getAccessToken()
{
$tenantId = config('app.office365_tenantId');
$clientId = config('app.office365_clientId');
$clientSecret = config('app.office365_clientSecret');
$tenantId = env('OAUTH_TENANT_ID');
$clientId = env('OAUTH_APP_ID');
$clientSecret = env('OAUTH_APP_PASSWORD');
$guzzle = new Client();
......@@ -28,14 +28,12 @@ class MicrosoftGraphController extends Controller
],
])->getBody()->getContents());
$accessToken = $token->access_token;
$headers = [
'Authorization' => 'Bearer ' . $token->access_token
];
//return dd($accessToken);
return $token->access_token;
}
public static function getAllUsers()
{
$accessToken = Api::getAccessToken();
// Create a Graph client
$graph = new Graph();
......@@ -49,40 +47,32 @@ class MicrosoftGraphController extends Controller
return dd($users);
}
public function getOffice365Groups()
public static function getAllGroups()
{
$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';
$accessToken = Api::getAccessToken();
$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());
// Create a Graph client
$graph = new Graph();
$graph->setAccessToken($accessToken);
$accessToken = $token->access_token;
$groups = $graph->createRequest('GET', '/groups')
->setReturnType(Model\Group::class)
->execute();
$headers = [
'Authorization' => 'Bearer ' . $token->access_token
];
//return dd($accessToken);
return dd($groups);
}
public static function getUser($id)
{
$accessToken = Api::getAccessToken();
// Create a Graph client
$graph = new Graph();
$graph->setAccessToken($accessToken);
$groups = $graph->createRequest('GET', '/groups')
->setReturnType(Model\Group::class)
$groups = $graph->createRequest('GET', '/users/'.$id)
->setReturnType(Model\User::class)
->execute();
......
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