Commit c3132a0a authored by Mark Siy's avatar Mark Siy

updates on office365 api

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