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