3

我有以下代码:

token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost:3000')
response = token.get('https://api.foursquare.com/v2/users/self/checkins', {:mode => :query})

问题是,无论我指定什么 :mode,我总是在 Authorization 标头中获得一个 Bearer 令牌。有问题的代码是一个私有的 set_token,它总是依赖于默认的:mode,它总是:header。

我用错了吗?

谢谢!

4

1 回答 1

5

oauth2 gem 如何在对象内部传递变量似乎存在问题,因此 mode 和 param_name 似乎在途中丢失了。该问题的解决方案是使用正确的参数创建一个新的 AccessToken 对象,而不是使用速记。此示例针对 Foursquares api 进行了测试,并且可以正常工作。

require "oauth2"

client = OAuth2::Client.new(
  "CLIENT_ID",
  "CLIENT_SECRET", 
  :authorize_url => "/oauth2/authorize", 
  :token_url => "/oauth2/access_token", 
  :site => "https://foursquare.com/"
)

puts client.auth_code.authorize_url(:redirect_uri => "http://localhost:4000")

code = gets.chomp

token = client.auth_code.get_token(code, :redirect_uri => "http://localhost:4000")

token = OAuth2::AccessToken.new(client, token.token, {
  :mode => :query,
  :param_name => "oauth_token",
})

response = token.get('https://api.foursquare.com/v2/users/self/checkins')

puts response.body
于 2011-09-05T14:46:20.267 回答