1

我目前正在使用 OAuth 2.0 来访问 Google 的阅读器 API。我已成功获得 URL 中返回的“代码”和“状态”。现在我正在使用 post 方法传入所需的参数以接收访问令牌。我已经摆弄了很长一段时间,我所拥有的是:

{ "error": "invalid_request" }

我的代码如下:

<?php 

session_start();

$code = $_GET['code'];
$state = $_GET['state'];

if ((!is_numeric($state)) || ($state != $_SESSION['state'])) {
    throw new Exception('Error validating state.');
}

$accessTokenExchangeUrl = 'https://accounts.google.com/o/oauth2/token';
$redirectUriPath = '/authentication.php';

$accessTokenExchangeParams = array(
    'code' => $code,
    'client_id' => 'xxxxx',
    'client_secret' => 'xxxxx',
    'redirect_uri' => (isset($_SERVER['HTTPS'])?'https://':'http://') . $_SERVER['HTTP_HOST'] . $redirectUriPath,
    'grant_type' => 'authorization_code'
    );


$goToUrl = $accessTokenExchangeUrl . '?' . http_build_query($accessTokenExchangeParams);

?> 

<!DOCTYPE HTML>
<html>
<head>
    <title></title>
</head>

<body>

    <form action=<?php echo $goToUrl; ?> method="post">
        <input type="submit" value="Click Me!">
    </form>

</body>

</html>

提前致谢!

4

1 回答 1

0

您是否尝试过将代码、client_id 等变量作为输入参数(在 POST 请求正文中)而不是在查询字符串中?谷歌的例子就是这样证明的

如果您遵循 OAuth 2 规范,则出于安全原因,它们不应出现在查询字符串中。

于 2012-09-26T03:40:57.150 回答