0

我完全按照教程进行操作,并收到以下消息:抱歉,登录时遇到问题。我们收到了错误的请求。可悲的是,除了这条信息,微软没有给我任何其他信息。

编辑:这是我使用的代码,其中 ID 和密码被屏蔽

主页.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;
    }
}
?>

授权.php

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

<p>Auth code: <?php echo $auth_code ?></p>
4

2 回答 2

1

我的问题都源于我未能在 Outlook 中注册我的重定向 URL,仅此而已。

于 2016-01-08T14:23:16.117 回答
0

添加这一行,它将起作用:

error_reporting(0);
于 2018-11-12T11:10:46.333 回答