OAuth gem 未签署请求 [英] OAuth gem not signing requests

查看:68
本文介绍了OAuth gem 未签署请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 ruby​​ gem 用于 OAuth (http://oauth.rubyforge.org/),但我无法使用它为我尝试访问的提供程序创建授权标头.

I'm using the ruby gem for OAuth (http://oauth.rubyforge.org/) and I can't get it to create the authorization header for the provider I'm attempting to hit.

这是我的代码:

consumer = OAuth::Consumer.new(auth[:consumer_key], auth[:consumer_secret], {
  :site   => 'http://api.rdio.com',
  :scheme => :header
})

access_token = OAuth::AccessToken.new(consumer)

ap access_token.post('/1', :method => 'search', :query => 'Robert', :types => 'User')

当请求发生时,调用中不存在标头.

When the requests happens, The header is not present in the call.

#<Net::HTTP::Post:0x7fbf149e91e0
    @body_data = nil,
    @header = {
                "accept" => [
            [0] "*/*"
        ],
            "user-agent" => [
            [0] "Ruby"
        ],
        "content-length" => [
            [0] "0"
        ],
          "content-type" => [
            [0] "application/x-www-form-urlencoded"
        ]
    },

我所指的标题如下所示:

The header I'm referring to is the one that looks like this:

<代码>的OAuth oauth_nonce = \ 225579211881198842005988698334675835446 \",oauth_signature_method = \ HMAC-SHA1 \",组oauth_token = \ token_411a7f \",oauth_timestamp = \ 1199645624 \",oauth_consumer_key = \ consumer_key_86cad9 \",oauth_signature=\"1oO2izFav1GP4kEH2EskwXkCRFg%3D\", oauth_version=\"1.0\"

推荐答案

看起来您正在尝试执行 2-legged oauth.看看此代码是否适合您.

Looks like you are trying to do 2-legged oauth. See if this code works for you.

更新代码示例

gem 'oauth'
require 'oauth'
require 'net/http'

consumer = OAuth::Consumer.new('ENTER_KEY', 'ENTER_SECRET', {        
                                :site   => 'http://api.rdio.com',        
                                :scheme => :header        
}) 

resp = consumer.request(:post, '/1/search', nil, {}, 'method=search&query=Robert&types=User', { 'Content-Type' => 'application/x-www-form-urlencoded' })
puts resp.code + "\r\n"
puts resp.body

添加了捕获的 http 流

POST /1/search HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: */*
User-Agent: OAuth gem v0.4.5
Content-Length: 37
Authorization: OAuth oauth_consumer_key="REDACTED_KEY", oauth_nonce="dwp8m2TGPHQNx3A7imLi7OkAULL7c0IWbTKefPXCsAY", oauth_signature="LxDZn6UNFLY%2FaXItu6MPK5a11js%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1330193449", oauth_version="1.0"
Connection: close
Host: api.rdio.com

method=search&query=Robert&types=UserHTTP/1.1 200 OK
X-Mashery-Responder: mashery-web1.LAX
Content-Type: application/json
Vary: Accept-Encoding
Vary: Accept-Language, Cookie
Content-Language: en
Cache-Control: no-cache
X-Version: 11.1
Accept-Ranges: bytes
Date: Sat, 25 Feb 2012 18:10:50 GMT
Server: Mashery Proxy
Content-Length: 2763
Connection: close

{"status": "ok", "result": {"person_count": 9603, "track_count": 93409, "number_results": 200, "playlist_count": 205, "results": ***TRUNCATED RESULTS FOR BREVITY***

这篇关于OAuth gem 未签署请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆