I have a php application that uses Office 365 oauth2 for authentication. Everything is working as it should using the code below. I need to also obtain the userid/email of the office 365 account used to authenticate. I haven't been able to figure out how to grab this. I have tried adding
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
But there is nothing returned for username. Here is the code I am using that works to authenticate and redirect:
$authorization_code = filter_input(INPUT_GET, 'code');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://login.microsoftonline.com/common/oauth2/token");
curl_setopt($ch, CURLOPT_POST, 1);
$client_id = "xxxxx";
$client_secret = 'yyyy';
curl_setopt($ch, CURLOPT_POSTFIELDS,
"grant_type=authorization_code&client_id=".$client_id."&redirect_uri=https%3A%2F%2Fwww.noname.com%2Fdev%2F&scope=openid%20profile%20offline_access%20User.Read%20Mail.Read&resource=https%3A%2F%2Fgraph.microsoft.com%2F&&code=".$authorization_code."&client_secret=".urlencode($client_secret));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$jsonoutput = json_decode($server_output, true);
$bearertoken = $jsonoutput['access_token'];
$refresh_token = $jsonoutput['refresh_token'];