Faching Issues While Trying To Get MS Email Address From oAuth Token

Viewed 26

Greeting, here is the folow:

i am using following code to get the auth code:

$AuthURL =  'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
$AuthURL .= '?client_id=7069_XXXX_d5';
$AuthURL .= '&response_type=code';
$AuthURL .= '&redirect_uri=https://www.example.com/mT1.php';
$AuthURL .= '&scope=' . urlencode('offline_access User.Read openid email profile MailboxSettings.ReadWrite');
$AuthURL .= '&response_mode=query';
header('Location: ' . filter_var($AuthURL, FILTER_SANITIZE_URL));

now, here is the code to get access token:

$pData = array(
        'client_id' => '706_XXXX_0d5',
        'code' => $_GET['code'],
        'redirect_uri' => 'https://www.example.com/mT1.php',
        'grant_type' => 'authorization_code',
        'client_secret' => '~XXXX',
        );
$postData = '';
foreach($pData as $key => $value)
{
    $postData .= "&$key=$value";
}
$to_ms = curl_init('https://login.microsoftonline.com/common/oauth2/v2.0/token');
curl_setopt($to_ms, CURLOPT_POST, true);
curl_setopt($to_ms, CURLOPT_RETURNTRANSFER, true);
curl_setopt($to_ms, CURLOPT_POSTFIELDS, $postData);
curl_setopt($to_ms, CURLOPT_SSL_VERIFYPEER, false);
$mReturn = curl_exec($to_ms);
curl_close ($to_ms);

this far, all is good, i get the access token successfully, now here is the code that i use to read the user email address:

$mAuth = json_decode($mReturn, true);
$to_ms = curl_init('https://graph.microsoft.com/oidc/userinfo');
curl_setopt($to_ms, CURLOPT_POST, true);
curl_setopt($to_ms, CURLOPT_RETURNTRANSFER, true);
curl_setopt($to_ms, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $mAuth['access_token'],
    'Content-Type: application/json', 
    'Accept: application/json'
));
curl_setopt($to_ms, CURLOPT_POSTFIELDS, '{}');
curl_setopt($to_ms, CURLOPT_SSL_VERIFYPEER, false);
$mReturn = curl_exec($to_ms);
curl_close ($to_ms);

but unluckily i am getting following error response:

{"error":{"code":"InvalidAuthenticationToken","message":"CompactToken parsing failed with error code: 80049217","innerError":{"date":"2022-09-17T07:08:00","request-id":"3aeda65e-b5c9-4479-8155-c226902dff6e","client-request-id":"3aeda65e-b5c9-4479-8155-c226902dff6e"}}}

so, even after many hours of trying, i can't see what i am doing wrong here :(

any help?

best regards

0 Answers
Related