douqiao1997 2015-04-21 13:22
浏览 178

尝试获取访问令牌请求Azure AD时invalid_grant

I am trying to get the access token from Azure Ad as explained the below link. I need to use the office 365 REST API.

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

Authorization code request is successfull and i am getting the code in return URL. But the Access token request is failing and the response i am getting as below.

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] => 
)

Below is my code:

$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);

Can you please tell me what am i doing wrong?

  • 写回答

1条回答 默认 最新

  • dousi6303 2015-04-21 14:02
    关注

    Take a look at this PHP sample: https://github.com/jasonjoh/php-calendar. Specifically, look at the getTokenFromAuthCode function in https://github.com/jasonjoh/php-calendar/blob/master/o365/Office365Service.php.

    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题