【问题标题】:Google API OAuth 2.0谷歌 API OAuth 2.0
【发布时间】:2012-09-26 11:40:57
【问题描述】:

我目前正在使用 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>

提前致谢!

【问题讨论】:

    标签: php api oauth-2.0 google-reader


    【解决方案1】:

    您是否尝试过将代码、client_id 等变量作为输入参数(在 POST 请求正文中)而不是在查询字符串中?谷歌示例demonstrate it that way

    如果您是following the OAuth 2 spec,则出于安全原因,它们不应出现在查询字符串中。

    【讨论】:

    • 谢谢,我创建了一个自定义 PHP CURL 函数来传递我的参数,现在一切正常:)
    猜你喜欢
    • 2015-02-14
    • 2017-07-31
    • 1970-01-01
    • 2014-10-06
    • 2015-01-10
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多