2013-03-07 115 views
9

我无法刷新Reddit访问令牌。无法刷新Reddit OAuth 2.0访问令牌

当我发送如下要求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,结果相同。

我错过了什么吗?

回答

20

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

用于在书签交易令牌清爽的必要参数是:

  1. CLIENT_ID
  2. client_secret
  3. grant_type(= refresh_token)
  4. refresh_token
  5. scope
  6. state
  7. duration
  8. REDIRECT_URI

您还需要基本的HTTP认证头与CLIENT_ID登录和client_secret作为密码。

我不得不查看reddit的source code来找出我的请求中缺少的东西......这么多的开发时间在琐碎的事情上丢失了。

+0

我相信这个bug现在已经修复了,你只需要grant_type和refresh_token参数。如果刷新令牌不是与client_id相同的应用程序,它将返回400 – Nathan 2017-02-07 06:23:37

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); 
相关问题