Код: Выделить всё
$appid = "xxxxxxxxxxxx"; //(MASKING DATA FOR OBVIOUS REASONS)
$tenantid = "xxxxxxxxxxxx"; //(MASKING DATA FOR OBVIOUS REASONS)
$secret = "xxxxxxxxxxxx"; //(MASKING DATA FOR OBVIOUS REASONS)
$login_url = "https://login.microsoftonline.com/" . $tenantid . "/oauth2/v2.0/authorize";
session_start();
$_SESSION['state'] = session_id();
echo "MS OAuth2.0 Demo ";
if (isset($_SESSION['msatg'])) {
echo "Authenticated " . $_SESSION["uname"] . " ";
echo 'Log Out';
} else {
echo 'You can Log In with Microsoft';
}
if (isset($_GET['action']) && $_GET['action'] == 'login') {
$params = array('client_id' => $appid, 'redirect_uri' => 'https://abc.xyz.com/sso/', 'response_type' => 'token', 'response_mode' => 'form_post', 'scope' => 'https://graph.microsoft.com/User.Read', 'state' => $_SESSION['state']);
header('Location: ' . $login_url . '?' . http_build_query($params));
}
if (array_key_exists('access_token', $_POST)) {
$_SESSION['t'] = $_POST['access_token'];
$t = $_SESSION['t'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $t,
'Content-type: application/json'
));
curl_setopt($ch, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rez = json_decode(curl_exec($ch), 1);
if (array_key_exists('error', $rez)) {
var_dump($rez);
die();
} else {
$_SESSION['msatg'] = 1; //auth and verified
$_SESSION['uname'] = $rez["displayName"];
$_SESSION['id'] = $rez["id"];
}
curl_close($ch);
header('Location: https://abc.xyz.com/sso/welcome.php');
}
if (isset($_GET['action']) && $_GET['action'] == 'logout') {
unset($_SESSION['msatg']);
header('Location: https://abc.xyz.com/sso/');
}
Я хочу>
Подробнее здесь: https://stackoverflow.com/questions/775 ... oauth2-php