Office 365身份验证:使用授权代码请求访问令牌

时间:2018-02-08 15:56:05

标签: azure office365 azure-active-directory

我正在处理此文档Authorize access to web applications using OAuth 2.0 and Azure Active Directory并且已经达到了成功登录时收到初始授权码的程度,但我很难获得授权令牌。据我了解,我需要在POST中发送代码以获取令牌。

我的POST请求是:

$code_received = $_GET['code']; //I get a code successfully

$url = 'https://login.microsoftonline.com';

$body="grant_type=authorization_code&client_id=myappid&code=" . $code_received . "&redirect_uri=https://mysite/auth.php&client_secret=***";

$options = array(
    'http' => array(
        'method'  => 'POST',
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'content' => $body
    )
);

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

我得到的结果是bool(false)....所以没有返回 - 既不是令牌也不是错误。谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:1)

您至少在resource中没有指定$body。这应该是您尝试使用的API的资源URI。它告诉Azure AD您要调用哪个API。

对于https://graph.microsoft.com的Microsoft Graph API。

对于Azure AD Graph API,它是https://graph.windows.net

相关问题