Outlook API - 我们收到了一个错误的请求

时间:2016-01-07 15:44:19

标签: php oauth outlook outlook-restapi

我正好关注this教程并收到以下消息:抱歉,我们在登录时遇到问题。 我们收到了一个糟糕的请求可悲的是,微软并没有给我任何其他信息。

编辑:这是我使用ID和密码掩码的代码

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>

2 个答案:

答案 0 :(得分:1)

我的问题都源于我未能使用Outlook注册我的重定向网址,就是这样。

答案 1 :(得分:0)

添加此行,它将起作用:

error_reporting(0);