0

I'm following this tutorial exactly and am getting the following message: Sorry, but we’re having trouble signing you in. We received a bad request. Miserably, Microsoft isn't giving me anything other than that message.

EDIT: here is the code I'm using with the ID's and passwords masked

home.php

<?php
session_start();
require('oauth.php');

$loggedIn = false;
$redirectUri = 'http://localhost:9999/outlook_cal03/authorize.php';
?>

<html>
<head>
    <title>PHP Mail API Tutorial</title>
</head>
<body>

    <?php
        if (!$loggedIn) {
    ?>
            <!-- User not logged in, prompt for login -->
            <p>Please <a href="<?php echo oAuthService::getLoginUrl($redirectUri)?>">sign in</a> with your Office 365 or Outlook.com account.</p>
            <?php
        }
        else {
            ?>
            <!-- User is logged in, do something here -->
            <p>Hello user!</p>
            <?php
        }
    ?>

</body>
</html>

oauth.php

<?php
class oAuthService {
    private static $clientId = "###################";
    private static $clientSecret = "################";
    private static $authority = "https://login.microsoftonline.com";
    private static $authorizeUrl = '/common/oauth2/v2.0/authorize?client_id=%1$s&redirect_uri=%2$s&response_type=code&scope=%3$s';
    private static $tokenUrl = "/common/oauth2/v2.0/token";

    // The app only needs openid (for user's ID info), and Mail.Read
    private static $scopes = array("openid", "https://outlook.office.com/mail.read");

    public static function getLoginUrl($redirectUri) {

        $redirectUri = $redirectUri;

        // Build scope string. Multiple scopes are separated
        // by a space
        $scopestr = implode(" ", self::$scopes);

        $loginUrl = self::$authority.sprintf(self::$authorizeUrl, self::$clientId, urlencode($redirectUri), urlencode($scopestr));

        error_log("Generated login URL: ".$loginUrl);
        return $loginUrl;
    }
}
?>

authorize.php

<?php
session_start();
$auth_code = $_GET['code'];
?>

<p>Auth code: <?php echo $auth_code ?></p>
lola_the_coding_girl
  • 843
  • 2
  • 10
  • 22

2 Answers2

1

My problems all stemmed from the fact that I had failed to register my redirect URL with Outlook, and that was it.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
lola_the_coding_girl
  • 843
  • 2
  • 10
  • 22
0

Add this line and it will work:

error_reporting(0);
shim
  • 9,289
  • 12
  • 69
  • 108