RAILS PRESS RUBY on RAILS, it’s DRY and COOL …

RSS Feed

RAILS PRESS RSS

Tag Cloud

はてなブックマーク - railspress.matake.jp の注目エントリー
象形文字くさび形文字ミイラそろばんの玉そろばんコーラン占いの板?象牙大英博物館
« 前のエントリ
iKnow! gem 0.2.2

Posted on
2008/12/27

Tags
OAuth, Yahoo, ノウハウ

Keywords


この記事をはてなブックマークに登録 この記事のはてなブックマーク数 この記事を livedoor クリップに登録この記事の livedoor クリップ数 このエントリを del.icio.us に追加
ブックマークに追加する

ruby-oauth で Yahoo! OAuth を使う方法

ruby-oauth で Yahoo! OAuth を使おうとしてだいぶはまったので、対応方法をメモ。

まずは oauth_parameter のうち、値が空のものは送らないようにしないと行けないようです。これは Yahoo! 側の問題かな?この問題を解決するには、OAuth::Client::Helper をオーバーライドします。

LANG : RUBY
  1. # Yahoo! Hacks (for OAuth2.1)
  2. class OAuth::Client::Helper
  3.   def oauth_parameters
  4.     { 'oauth_consumer_key'     => options[:consumer].key,
  5.       'oauth_token'            => options[:token] ? options[:token].token : '',
  6.       'oauth_signature_method' => options[:signature_method],
  7.       'oauth_session_handle'   => options[:oauth_session_handle] ? options[:oauth_session_handle] : '',
  8.       'oauth_timestamp'        => timestamp,
  9.       'oauth_nonce'            => nonce,
  10.       'oauth_version'          => '1.0' }.reject { |k,v| v == "" }
  11.   end
  12. end

次に、Yahoo! OAuth では1時間ごとに token を再発行したり、その際に session_handle を使ったりするので、ruby-oauth の OAuth::AccessToken と OAuth::RequestToken をこれらの属性に対応させる必要があります。

今はとりあえず Yahoo 用に AccessToken と RequestToken クラスを用意してやってます。多分そのうち ruby-oauth 側が対応するはずなので、それまではこれで。

LANG : RUBY
  1. class OAuth::AccessToken::Yahoo <OAuth::AccessToken
  2.   attr_accessor :session_handle, :expires_in, :authorization_expires_in
  3.   def initialize(consumer, token, secret, options = {})
  4.     super(consumer, token, secret)
  5.     @session_handle           = options[:session_handle]
  6.     @expires_in               = options[:expires_in]
  7.     @authorization_expires_in = options[:authorization_expires_in]
  8.   end
  9. end
  10.  
  11. class OAuth::RequestToken::Yahoo <OAuth::RequestToken
  12.   def get_access_token(options={})
  13.     response = consumer.token_request(consumer.http_method, consumer.access_token_path, self, options)
  14.     access_token = OAuth::AccessToken::Yahoo.new(
  15.       consumer,
  16.       response[:oauth_token],
  17.       response[:oauth_token_secret],
  18.       { :session_handle           => response[:oauth_session_handle],
  19.         :expires_in               => response[:oauth_expires_in],
  20.         :authorization_expires_in => response[:oauth_authorization_expires_in] } )
  21.   end
  22. end

あとはtoken の長さが 255 文字を超える(常に670文字?)のと、:session_handle, :expires_in, :authorization_expires_in の3つの属性が追加されるので、DB にそれらを反映させるのを忘れずに。


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


コメントはこちらから




使用可能タグ: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


« 前のエントリ
iKnow! gem 0.2.2