9

我无法刷新 Reddit 访问令牌。

当我发送以下请求时https://ssl.reddit.com/api/v1/access_token

Content-Type: application/x-www-form-urlencoded
Authorization: #####
client_secret=#####&grant_type=refresh_token&client_id=#####&refresh_token=#####

我得到状态200,但内容是{"error": "invalid_request"}

根据OAuth 2.0 规范Reddit 规范,我做的一切都是正确的。

我也尝试过没有client_idclient_secret相同的结果。

我错过了什么吗?

4

2 回答 2

22

Reddit 的 OAuth 实现非常独特(而且不是很好)。

在 reddit 上刷新令牌的必要参数是:

  1. client_id
  2. client_secret
  3. grant_type (=refresh_token)
  4. 刷新令牌
  5. 范围
  6. 状态
  7. 期间
  8. 重定向uri

您还需要使用client_id作为登录名和client_secret作为密码的基本 HTTP 身份验证标头。

我不得不查看 reddit 的源代码,以找出我的请求中缺少什么……在琐碎的事情上浪费了太多的开发时间。

于 2013-03-25T06:49:52.313 回答
2

如果有人正在寻找更明确的答案:

这是我在 PHP 中的做法。

    $authorizeUrl = 'https://ssl.reddit.com/api/v1/access_token';
    $clientId = "YOUR_CLIENT_ID";
    $clientSecret = "YOUR_CLIENT_SECRET";

    $post = array(
        "client_id" => $clientId,
        "client_secret" => $clientSecret,
        "grant_type" => "refresh_token",
        "refresh_token" => "STORED_REFRESH_TOKEN_VALUE",
        "scope" => "identity",
        "state" => "WHATEVER_VALUE",
        "duration" => "temporary",          
        "redirect_uri" => "https://example.com/reddit",
    );

    $payload = http_build_query($post);

    $ch = curl_init($authorizeUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);        

    print_r($result);
于 2014-02-07T00:42:52.170 回答