Office 365 oauth2-获取用户ID /电子邮件

时间:2019-03-05 18:20:32

标签: php oauth-2.0 office365

我有一个使用Office 365 oauth2进行身份验证的php应用程序。使用下面的代码,一切都可以正常进行。我还需要获取用于身份验证的Office 365帐户的用户ID /电子邮件。我还没弄清楚如何抓住这个机会。我尝试添加

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

但是没有返回用户名。这是我正在使用的可用于身份验证和重定向的代码:

    $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'];

0 个答案:

没有答案