ruby-oauth で Yahoo! OAuth を使う方法
ruby-oauth で Yahoo! OAuth を使おうとしてだいぶはまったので、対応方法をメモ。
まずは oauth_parameter のうち、値が空のものは送らないようにしないと行けないようです。これは Yahoo! 側の問題かな?この問題を解決するには、OAuth::Client::Helper をオーバーライドします。
-
# Yahoo! Hacks (for OAuth2.1)
-
class OAuth::Client::Helper
-
def oauth_parameters
-
{ 'oauth_consumer_key' => options[:consumer].key,
-
'oauth_token' => options[:token] ? options[:token].token : '',
-
'oauth_signature_method' => options[:signature_method],
-
'oauth_session_handle' => options[:oauth_session_handle] ? options[:oauth_session_handle] : '',
-
'oauth_timestamp' => timestamp,
-
'oauth_nonce' => nonce,
-
'oauth_version' => '1.0' }.reject { |k,v| v == "" }
-
end
-
end
次に、Yahoo! OAuth では1時間ごとに token を再発行したり、その際に session_handle を使ったりするので、ruby-oauth の OAuth::AccessToken と OAuth::RequestToken をこれらの属性に対応させる必要があります。
今はとりあえず Yahoo 用に AccessToken と RequestToken クラスを用意してやってます。多分そのうち ruby-oauth 側が対応するはずなので、それまではこれで。
-
class OAuth::AccessToken::Yahoo <OAuth::AccessToken
-
attr_accessor :session_handle, :expires_in, :authorization_expires_in
-
def initialize(consumer, token, secret, options = {})
-
super(consumer, token, secret)
-
@session_handle = options[:session_handle]
-
@expires_in = options[:expires_in]
-
@authorization_expires_in = options[:authorization_expires_in]
-
end
-
end
-
-
class OAuth::RequestToken::Yahoo <OAuth::RequestToken
-
def get_access_token(options={})
-
response = consumer.token_request(consumer.http_method, consumer.access_token_path, self, options)
-
access_token = OAuth::AccessToken::Yahoo.new(
-
consumer,
-
response[:oauth_token],
-
response[:oauth_token_secret],
-
{ :session_handle => response[:oauth_session_handle],
-
:expires_in => response[:oauth_expires_in],
-
:authorization_expires_in => response[:oauth_authorization_expires_in] } )
-
end
-
end
あとはtoken の長さが 255 文字を超える(常に670文字?)のと、:session_handle, :expires_in, :authorization_expires_in の3つの属性が追加されるので、DB にそれらを反映させるのを忘れずに。












この記事がお役に立ちましたら、一言コメントもらえると嬉しいですm_ _m