php - 尝试获取访问 token 请求 Azure AD 时 invalid_grant

标签 php rest azure office365

我正在尝试从 Azure Ad 获取访问 token ,如以下链接所述。我需要使用 Office 365 REST API。

msdn.microsoft.com/en-us/library/azure/dn645542.aspx

授权代码请求成功,我在返回 URL 中获取代码。但访问 token 请求失败,我得到的响应如下。

stdClass Object
(
    [error] => invalid_grant
    [error_description] => AADSTS70002: Error validating credentials. AADSTS70000: The provided access grant is invalid or malformed.
Trace ID: baf9f98a-655d-48e1-bc30-021a7d57effa
Correlation ID: 5030b57f-42e2-403a-ab3c-cfc970d886e2
Timestamp: 2015-04-21 12:58:00Z
    [error_codes] => Array
        (
            [0] => 70002
            [1] => 70000
        )

    [timestamp] => 2015-04-21 12:58:00Z
    [trace_id] => baf9f98a-655d-48e1-bc30-021a7d57effa
    [correlation_id] => 5030b57f-42e2-403a-ab3c-cfc970d886e2
    [submit_url] => 
    [context] => 
)

下面是我的代码:

$url = "https://login.windows.net/<tenant-id>/oauth2/token";
            $postUrlData = "grant_type=authorization_code&client_id=$clientId&code=$authCode&redirect_uri=$redirectUri&client_secret=$secretKey";
            $headers = array(
                'Content-type: application/x-www-form-urlencoded',
                'Content-length: '. strlen($postUrlData)
            );
            //echo $postUrlData;die;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS , $postUrlData);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch);            
            $res = json_decode($result);            
            curl_close($ch);

你能告诉我我做错了什么吗?

最佳答案

看看这个 PHP 示例:https://github.com/jasonjoh/php-calendar 。具体来说,查看 https://github.com/jasonjoh/php-calendar/blob/master/o365/Office365Service.php 中的 getTokenFromAuthCode 函数。 。

关于php - 尝试获取访问 token 请求 Azure AD 时 invalid_grant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29773459/

相关文章:

Azure DevOps PowerShell - 我可以为库组变量构建变量字符串吗

php - 带有 "WHERE"问题的小型 php fetch 查询

php - 获取自定义格式的json数据

javascript - 在Backbone js中永久引用另一条数据

spring - 无法使用 Spring MockMvc 将正文附加到我的 POST 请求

azure - 术语 'New-AzResourceGroupDeployment' 未被识别为 cmdlet、函数的名称

php - 如何获取href属性的值?

php - 当包含标题时,在 PHP 中,您如何添加内容?

javascript - Ember-data: "no model found"在那里

Azure注册应用程序错误: The user or administrator has not consented to use the application with ID