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!