douzhe3516 2019-02-11 15:25
浏览 100

Office365独立的php app授权

I have a standalone PHP webapp that I am tasked with adding Office365 authorization to in order for only authorized users to access.

I have registered the application in the "Application Registration Portal" in our Office365 environment and received an Application ID. I set the Redirect URL to the application on my localhost: http://localhost/MyApp.

Within my initial page for the Webapp (login.php), I have the following redirect:

Location:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id..... 

That piece works successfully as it will redirect the user to the Office365 login page, and after a user enters credentials, takes them to the index page (index.php).

In my index page, I have the following:

    <?php 
    $client_id = 'xxxx';   // application ID masked 
    $client_secret = 'xxxx';  //password/public key masked  
    $redirect_uri= "http://localhost/WebApp";
    $resource = "https://localhost/WebApp"; 
    $authorization_code = filter_input(INPUT_GET, 'code');          

    if(!$authorization_code){
        die('something went wrong!');
    }
    $url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
    $data = array(
        'client_id' => $client_id,
        'code' => $authorization_code,
        'redirect_uri' => $redirect_uri,
        'resource' => $resource,
        'client_secret' => $client_secret
    );


    $options = array(
        'http' => array(
            'header'  => "Content-type: application/json
",
            'method'  => 'POST',
            'content' => json_encode($data)
        )
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

      //echo $authorization_code; 

    var_dump($result);
    ?>  

When I run this, I get the following error after the redirect:

Warning: file_get_contents(https://login.microsoftonline.com/common/oauth2/v2.0/token): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\xampp\htdocs\MyApp\index.php on line 29

I also can navigate in the browser directly to the index.php page, which defeats the purpose of the authentication.

Any help is most appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog