Simo OM
Simo OM

Reputation: 139

How to Get Access Token and connect user without redirection from Microsoft Graph API using PHP

I tried this code but not get access token, but not working I want to sync my outlook 365 calendar events with my system. My system is a background service, not an application, therefore i can't provide a login screen for the user to approve authorization.

I'm following this link in order to get an access token

Get access without a user

$guzzle = new \GuzzleHttp\Client();
$url='https://login.microsoftonline.com/'.config('azure.tenantId').'/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
    'form_params' => [
       'grant_type'    => 'client_credentials',
       'client_id'     => config('azure.appId'),
       'client_secret' => config('azure.appSecret'),
       'scope'         => config('azure.scopes'),
       'username'      => "[email protected]",
       'password'      => "password",
    ],
])->getBody()->getContents());
$accessToken = $token->access_token;

//Code to get data user form Microsoft Graph API

$graph = new Graph();
$graph->setAccessToken($token->access_token);

$user = $graph->createRequest('GET', '/me?$select=displayName,mail,mailboxSettings,userPrincipalName')
    ->setReturnType(Model\User::class)
    ->execute();

$tokenCache = new TokenCache();
$tokenCache->storeTokens($accessToken, $user);

enter image description here

Upvotes: 0

Views: 2805

Answers (1)

Simo OM
Simo OM

Reputation: 139

The solution to Login direct without redirection The documentation is clear, but I did not understand it well, but I finally found the solution after looking at the documentation step by step well https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc#authorization-request

try {
    $guzzle = new \GuzzleHttp\Client();
    $url = 'https://login.microsoftonline.com/'.config('azure.tenantId').'/oauth2/v2.0/token';
    $token = json_decode($guzzle->post($url, [
    'form_params' => [
        'grant_type'    => 'password',
        'client_id'     => config('azure.appId'),
        'client_secret' => config('azure.appSecret'),
        'scope'         => config('azure.scopes'),
        'username'      => "[email protected]",
        'password'      => "password",
    ],
])->getBody()->getContents());

$graph = new Graph();
$graph->setAccessToken($token->access_token);

$user = $graph->createRequest('GET', '/me?$select=displayName,mail,mailboxSettings,userPrincipalName')
    ->setReturnType(Model\User::class)
    ->execute();

$token = new \League\OAuth2\Client\Token\AccessToken(json_decode(json_encode($token), true));
$tokenCache = new TokenCache();
$tokenCache->storeTokens($token, $user);

return redirect('/');
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
    return redirect('/')->with('error', 'Error requesting access token')->with('errorDetail', json_encode($e->getResponseBody()));
}

enter image description here

enter image description here

Upvotes: 1

Related Questions