2016-09-21 87 views
1

我正在使用oauth的rest-client,oauth標題正在生成,但hmac-sha1簽名無效。Ruby Oauth Gem HMAC-SHA1 401無效簽名

服務器不接受簽名編碼,返回401 oauth_problem signature_invalid。

oauth_signature="9aOk2oM2%2FczpqHBJ95MhcF%2Bp6XU%3D", 
oauth_signature_method="HMAC-SHA1" 

在郵遞員中運行「未編碼OAuth簽名」未選中工作正常。 當打開此選項時,郵遞員會提供相同的401無效簽名。

這是編碼的問題,有沒有類似的選項oauth寶石,我該如何設置它?

require 'rubygems' 

require 'oauth' 
require 'rest-client' 

# auth keys read in from file 
base_uri = 'http://dockerized-magento.local' 
base_path = 'api/rest' 
base_url = base_uri + '/' + base_path + '/customers' # the fix 

@consumer=OAuth::Consumer.new auth['consumer_key'], 
           auth['consumer_secret'], 
           {:site => base_url }  # the fix 
# this was the error 
#        {:site=> base_uri + base_path} 

# Create the access_token for all traffic 
access_token = OAuth::AccessToken.new(@consumer, 
             auth['token'], auth['token_secret']) 

RestClient.add_before_execution_proc do |req, params| 
    access_token.sign! req 
end 

response = RestClient::Request.execute(method: :get, url: url, 
             timeout: 60, 
             headers: {'Cache-Control' => 'no-cache'}) 

其餘客戶端2.0使用OAuth寶石0.5.1。,2.2.2紅寶石,不使用紅寶石上導軌

回答

1

這個問題是我不正確地設置的基本URL。我看到:Creating a signature

基本URL是請求所針對的URL,減去任何查詢字符串或散列參數。在這裏使用正確的協議很重要,所以請確保URL的「https://」或「http://」部分與發送到API的實際請求匹配。我調整我的BASE_URL相應

base_uri = 'http://dockerized-magento.local' 
base_path = 'api/rest' 

base_url = base_uri + '/' + base_path + '/customers' 

,改變

 @consumer=OAuth::Consumer.new auth['consumer_key'], 
          auth['consumer_secret'], 
          {:site=> base_url} 

我更新上面的代碼的清晰度。

相關問題