douji6667 2019-03-05 18:20
浏览 262

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

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'];
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料