2013-06-04 282 views
1

我難倒。我試圖手動獲取刷新令牌來爲我在Mirror API上使用Perl加載訪問令牌,並且它不斷給我提供憑據錯誤。當我在PHP示例代碼中加載確切的HTTP請求(我已經打印出要比較的HTTP)時,同樣的refresh_token可以正常工作。refresh_token憑據無效錯誤

這裏是我的Perl的HTTP請求:

* POST https://accounts.google.com/o/oauth2/token 主持人:accounts.google.com 的User-Agent:的libwww-perl的/ 6.02 的Content-Length:175 內容類型:應用程序/ X- WWW窗體-urlencoded 的client_id = client_id_goes_here & client_secret = client_secret_goes_here & refresh_token = refresh_token_goes_here & grant_type = refresh_token *

下面是在同一refresh_token的PHP:

* POST/O /的oauth2 /令牌HTTP/1.1 內容類型:application/X WWW的窗體-urlencoded 內容長度:175 CLIENT_ID = client_id_goes_here & client_secret = client_secret_goes_here & refresh_token = refresh_token_goes_here & grant_type = refresh_token *

我的Perl是這樣的:

my $auth_response = $ua->request(POST 'https://accounts.google.com/o/oauth2/token', 
      'Host'   => 'accounts.google.com', 
      'Content_Type' => 'application/x-www-form-urlencoded', 
      'Content'  => [ 
       'client_id'   => $client_id, 
       'client_secret'  => $client_secret, 
       'refresh_token'  => $credentials->{'refresh_token'}, 
       'grant_type'  => 'refresh_token', 
      ], 
     ); 

HELP! :-)

回答

1

看起來你正在使用LWP。我砍了this quick example,它使用LWP從開始到令牌刷新跳舞與Google的OAuth 2.0舞蹈。

根據我的實驗,還有你的代碼到目前爲止看起來是正確的。下面是我用來刷新我的訪問令牌的確切代碼:

my $auth_response = $ua->request(POST 'https://accounts.google.com/o/oauth2/token', 
      'Host'   => 'accounts.google.com', 
      'Content_Type' => 'application/x-www-form-urlencoded', 
      'Content'  => [ 
       'client_id'   => $client_id, 
       'client_secret'  => $client_secret, 
       'refresh_token'  => $refresh_token, 
       'grant_type'  => 'refresh_token', 
      ], 
     ); 

如果你還在觀察一個錯誤,請嘗試克隆即回購,填充你的CLIENT_ID和client_secret,如果問題仍然存在有看頭。如果是這樣,請分享print Dumper($auth_response);的結果,這將提供大量有用的信息。

此外,Perl不是谷歌正式支持的語言,但它看起來像社區已經過去了:有一個開源的Perl client library。我以前從未使用過它,但您可能想要查看它。

+1

我相信這是我的最終問題。我正在輸出錯誤的響應標題(來自代碼中的先前請求),這就是我看到錯誤的原因。打印出正確的響應頭文件修復了它。你是對的 - 這是正確的代碼!無論如何,我都會把它留在這裏,以防其他人在尋找代碼來授權Google Glass。 –