I’m trying to use accessToken to send mail in Microsoft 365.
But when decode accessToken, I realize that it does not have any scopes
inside.
So I guess that the reason why I can’t use this accessToken to send Mail.
This is my code to get accessToken before use it to send mail.
<?php
require 'vendor/autoload.php';
use GuzzleHttpClient;
use MicrosoftGraphGraph;
$guzzle = new Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'scope' => 'https://graph.microsoft.com/.default',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
function base64UrlDecode($input) {
$remainder = strlen($input) % 4;
if ($remainder) {
$addlen = 4 - $remainder;
$input .= str_repeat('=', $addlen);
}
return base64_decode(strtr($input, '-_', '+/'));
}
function decodeJwt($jwt) {
$parts = explode('.', $jwt);
if (count($parts) === 3) {
list($header, $payload, $signature) = $parts;
$decodedPayload = base64UrlDecode($payload);
return json_decode($decodedPayload, true);
}
return null;
}
$decodedToken = decodeJwt($accessToken);
if ($decodedToken) {
echo "Decoded Token:n";
print_r($decodedToken);
} else {
echo "Failed to decode token.";
}
Decoded Token:
Array
(
[aud] => https://graph.microsoft.com
[iss] => https://sts.windows.net/tenant_id/
[iat] => 1721285822
[nbf] => 1721285822
[exp] => 1721289722
[aio] => E2dgYDh/MUzO53mMQvZEb9Xk23da1==
[app_displayname] => WordPress send mail by PHP library
[appid] => client_id
[appidacr] => 1
[idp] => https://sts.windows.net/150115f9-9fg2-468e-8256-31864c946d38/
[idtyp] => app
[oid] => information
[rh] => information.
[sub] => information
[tenant_region_scope] => AS
[tid] => tenant_id
[ver] => 1.0
[wids] => Array
[0] => 0997a1d0-0d1d-4acb-b408-d5ca73121e90
)
[xms_idrel] => 7 4
[xms_tcdt] => 1691395903
)
Could anyone help me to know what is the issue or what are the missing roles?