douchixu3686 2017-06-02 14:00
浏览 71
已采纳

PHP:500服务器错误 - 请求Microsoft Azure Active Directory访问的访问令牌

I'm following this documentation: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code

I have just completed this step (by simply visiting the URL below):

https://login.microsoftonline.com/{tenant}/oauth2/authorize?client_id={client_id}&response_type=code

Which redirected me to my redirect URL with code query string attached:

https://example.com/?code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCm.................................

At the end of the code query string above, there was an additional session_state parameter.

The next step outlined in the link at the top of this question says to make a POST request, but I'm having trouble forming this call.

Here's what the docs represent being an example:

Authorization code to request an access token example

How would I form and call this request in PHP (without using cURL)?

Here's my attempt, but I don't know whether or not I'm correct:

$url = 'https://login.microsoftonline.com/{tenant}/oath2/token';

$data = array( 'grant_type' => 'authorization_code',
               'client_id' => '2d4d11a2-f814-46a7-890a-274a72a7309e',
               'code' => 'AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCm...................',
               'redirect_uri' => 'https://example.com',
               'resource' => 'https://graph.microsoft.com',
               'client_secret' => '{client_secret}' );

$options = array(
    'http' => array(
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

$var_dump($result);

UPDATE: The code above (when executed) returns a 500 Internal Server Error.

Also, I don't know whether or not I should be adding session_state (mentioned above) into the POST call.

  • 写回答

1条回答 默认 最新

  • drxnfdx798517235 2017-06-02 14:02
    关注

    The URL should be https://login.microsoftonline.com/{tenant}/oauth2/token.

    You have to add the client secret to the request, as well as the resource.

    If you are trying to call Microsoft Graph for example, the resource should be https://graph.microsoft.com.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系