【问题标题】:Ruby Oauth Gem HMAC-SHA1 401 Invalid SignatureRuby Oauth Gem HMAC-SHA1 401 无效签名
【发布时间】:2016-09-21 18:09:40
【问题描述】:

我正在使用带有 oauth 的 rest-client,正在生成 oauth 标头,但 hmac-sha1 签名无效。

服务器不接受签名编码,返回 401 oauth_problem signature_invalid。

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

在未选中“编码 OAuth 签名”的情况下在 Postman 中运行可以正常工作。 开启此选项后,Postman 给出相同的 401 无效签名。

这是编码问题吗,oauth gem 是否有类似的选项,我该如何设置?

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'})

rest-client 2.0 与 oauth gem 0.5.1.,ruby 2.2.2,不使用 ruby​​-on-rails

【问题讨论】:

    标签: ruby oauth rest-client


    【解决方案1】:

    问题是我错误地设置了基本网址。我看到了这个: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}
    

    为了清楚起见,我正在更新上面的代码。

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      相关资源
      最近更新 更多