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.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)