<?php // Start a session and load the OAuth library. session_start(); require_once __DIR__ . '/oauth-client/src/microsoft.class.php'; // Delete the access token if needed. if (isset($_GET["del_token"]) && isset($_SESSION["m_token"])) { unset($_SESSION["m_token"]); } // Create a new Microsoft object. $microsoft = new OAuthMicrosoft("0000000000000000", "0000000000000000000000000000000000000000", array("session_prefix" => "m_")); // Output a Login Button. echo $microsoft->loginButton("Login with Microsoft Account", "https://example.com/microsoft-1/code.php", array("wl.signin", "wl.basic")); // If an access token exists (OAuth2::accessToken() does not return null), fetch the current user's data. if ($microsoft->accessToken() != null) { // The user is logged in, you can do whatever you like here. // In this example we just print the profile data, along with the profile picture and permissions. $profile = $microsoft->userProfile(); echo "<pre>" . print_r($profile, true) . "</pre><br /><br />\n\n"; } else { echo "You have not granted access to Microsoft. Click the link above.\n"; }
<?php // Start a session and load the OAuth library. session_start(); require_once __DIR__ . '/oauth-client/src/microsoft.class.php'; // Create a new Microsoft object. $microsoft = new OAuthMicrosoft("0000000000000000", "0000000000000000000000000000000000000000", array("session_prefix" => "m_")); // Try to fetch an access token with the code in $_GET["code"]. Also check the state in $_GET["state"]. try { $microsoft->getAccessTokenFromCode("http://example.com/microsoft-1/code.php"); // -------- // If we got here, no error was thrown and an access token was successfully retrieved from the code. // Output the code and access token. echo "Success! Click the link at the bottom of the page to return home and fetch data using the access token.<br />\n"; echo "Code: " . htmlspecialchars($_GET["code"]) . "<br />\n"; echo "Access Token: " . htmlspecialchars($microsoft->accessToken()) . "\n<br /><br />\n\n"; } catch (Exception $error) { echo "Error - Click the link at the bottom of the page to return home and try again: " . $error->getMessage() . "\n<br /><br />\n\n"; } // Output a link to the homepage to fetch data using the access token. echo "<a href=\"./\">Home</a>\n";