2013-03-07 117 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); 
相關問題