【问题标题】:Unable to refresh Reddit OAuth 2.0 access token无法刷新 Reddit OAuth 2.0 访问令牌
【发布时间】:2013-02-23 07:41:20
【问题描述】:

我无法刷新 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 specReddit spec 我做的一切都是正确的。

我也尝试过不使用client_idclient_secret,结果相同。

我错过了什么吗?

【问题讨论】:

    标签: oauth-2.0 reddit


    【解决方案1】:

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

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

    1. client_id
    2. client_secret
    3. grant_type (=refresh_token)
    4. 刷新令牌
    5. scope
    6. state
    7. duration
    8. redirect_uri

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

    我不得不查找 reddit 的 source code 以找出我的请求中缺少什么......在琐碎的事情上浪费了太多的开发时间。

    【讨论】:

    • 我相信这个bug现在已经修复了,你只需要grant_type和refresh_token参数。如果刷新令牌与 client_id 不是同一应用程序,它将返回 400
    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2015-04-01
      • 2016-02-19
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2014-09-22
      • 2012-07-11
      • 2014-01-01
      • 2014-07-30
      相关资源
      最近更新 更多