èªè¨¼APIãããããRubyã§ä½¿ã
ã¯ã¦ãªèªè¨¼API
Rubyã§ã¯ã¦ãª
Hatena::API::Authã®Rubyçãid:secondlifeãããå ¬éãã¦ããã¦ãã
gem install hatenaapiauth
require 'rubygems' require 'hatena/api/auth' require 'cgi' cgi = CGI.new params = { :api_key=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", :secret=>"xxxxxxxxxxxxxxxx" } auth = Hatena::API::Auth.new(params) begin hatena = auth.login(cgi['cert']) # hatena = { # "name" => "taslam", # "image_url" => "http://www.hatena.ne.jp/users/ta/taslam/profile.gif", # "thumbnail_url" => "http://www.hatena.ne.jp/users/ta/taslam/profile_s.gif" # } rescue Hatena::API::AuthError => e # èªè¨¼å¤±æ # auth.uri_to_login.to_s ã«ãªãã¤ã¬ã¯ãã§ã¯ã¦ãªã®èªè¨¼ãã¼ã¸ã«ã end
livedoor Auth
Rubyã§livedoor Auth
gem install livedoorauth
require 'rubygems' require 'livedoorauth' require 'cgi' params = { :app_key=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", :secret=>"xxxxxxxxxxxxxxxx" } cgi = CGI.new auth = Livedoor::API::Auth.new(params) begin user = auth.validate_response(cgi.query_string) # user = { # :userhash => 'ã¦ã¼ã¶ãèå¥ããã¢ããªæ¯ã«ä¸æãªæåå', # :token => 'LivedoorIDåå¾ã«ç¨ããtoken', # :userdata => 'èªè¨¼URLçææã«æå®ããå¼ç¶ããã¼ã¿', # } # èªè¨¼æå # URLçææã«:perms => 'id'ãæå®ãã¦ããã°LivedoorIDãåå¾å¯è½ã # livedoor_id = auth.get_livedoor_id(user) rescue # èªè¨¼å¤±æ # ãã°ã¤ã³ç¨URIã¤ã³ã¹ã¿ã³ã¹åå¾ # Livedoorã¸ã®ãã°ã¤ã³ãã¢ããªã±ã¼ã·ã§ã³ã®èªè¨¼æ å ±å©ç¨ã®è¨±å¯ãªã© # èªè¨¼ã§ããããããããè¨å®ãã¦ããã³ã¼ã«ããã¯URLã«ãªãã¤ã¬ã¯ãè¦æ±çºç # userdataã¯ãã®ã¾ã¾ã³ã¼ã«ããã¯URLã«ä»å ããã¦å¸°ã£ã¦ãã # :permsã®ããã©ã«ãã¯userhashãidã«ããã¨ãLivedoorIDãåå¾ã§ããããã«ãªãã login_uri = auth.uri_to_login(:userdata => 'data') # login_uri ã«ãªãã¤ã¬ã¯ãã§ãLivedoorã«èªè¨¼ãã¼ã¸ã«ã end
YahooBBAuth
http://developer.yahoo.com/auth/
Yahoo!Japanã®ã»ãã¯ãç¾å¨ã®ã¨ããä¸è¬å
¬éã¯ãã¦ããªã模æ§(´ã»Ïã»ï½)
GoogleAccountAuthenticationã使ãã
http://code.google.com/apis/accounts/AuthForWebApps.html
Rubyã§GoogleAccountAuthentication
GoogleCalendarãµã¼ãã¹ã§ã®èªè¨¼ãå©ç¨ããã
ä»åã¯ã©ã¤ãã©ãªããã£ãã®ã§ãå©ç¨ããã¦ããã ããã
gem install gcalapi
begin GoogleCalendar::Service.new('[email protected]', 'password') # ã«ã¬ã³ãã¼åå¾ã¡ã½ããå ã§èªè¨¼ãã¦ãã®ã§å©ç¨ã # ç´æ¥èªè¨¼ã¡ã½ãããå©ããªããsrv.instance_eval('auth') srv.calendars rescue GoogleCalendar::AuthenticationFailed => e # èªè¨¼å¤±æ end
Flickr
http://flickr.com/services/api/auth.howto.web.html
Rubyã§Flickrèªè¨¼
èªè¨¼ã®ããã®ã使ãæãã©ã¤ãã©ãªãè¦ã¤ãããªãã£ãã®ã§ç°¡åã«ã¤ãã£ã¦ã¿ãã
flickr/api/auth.rb
require 'digest/md5' require 'uri' require 'open-uri' require 'rexml/document' module Flickr module API class AuthError < RuntimeError;end class Auth def initialize(params) @api_key = params[:api_key].to_s @secret = params[:secret].to_s end def login(frob) return nil if frob.to_s.empty? uri = service_uri('rest') params = { :api_key => @api_key, :method => 'flickr.auth.getToken', :frob => frob.to_s } uri.query = params.merge!(:api_sig => sigunature(params)).collect { |key, value| "#{key}=#{value}"}.join('&') doc = REXML::Document.new(uri.read) rsp = doc.elements['//rsp'] if rsp.attributes['stat'] == 'fail' raise AuthError.new(rsp.elements['err'].attributes['msg']) end auth = doc.elements['//auth'] { :token => auth.elements['token'].text, :perms => auth.elements['perms'].text, :user => { :nsid => auth.elements['user'].attributes['nsid'], :username => auth.elements['user'].attributes['username'], :fullname => auth.elements['user'].attributes['fullname'], } } end def uri_to_login(perm = 'read') uri = service_uri('auth') params = { :api_key => @api_key, :perms => perm } uri.query = params.merge!(:api_sig => sigunature(params)).collect { |key, value| "#{key}=#{value}"}.join('&') uri end private def service_uri(path = 'auth') URI.join('http://flickr.com/services/', path) end def sigunature(params) Digest::MD5.hexdigest(@secret+params.sort_by{|key, value| key.to_s }.flatten.join).to_s end end end end
ãããªãããã«ä½¿ãã
require 'flickr/api/auth' require 'cgi' params = { :api_key=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", :secret=>"xxxxxxxxxxxxxxxx" } cgi = CGI.new auth = Flickr::API::Auth.new(params) if info = auth.login(cgi['frob']) # èªè¨¼æå # infoã®ä¾ # info => { # :user => { # :nsid=>"000000@N00", # :username=>"taslam", # :fullname=>"" # }, # :token=>"72157603867040929-8ffc565113df0adc", # :perms=>"read" # } else # èªè¨¼å¤±æ # auth.uri_to_login.to_s ã«ãªãã¤ã¬ã¯ãè¦æ± end
Jugem
Rubyã§Jugemèªè¨¼
llameradaさんがライブラリを公開してくれてるã®ã§ããã使ããã¦ããã ãã
require 'jugem/auth' require 'cgi' params = { :api_key => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" :secret => "xxxxxxxxxxxxxxxx", :callback_url => "ç»é²ããURLãå«ãã³ã¼ã«ããã¯å ããã®ä¾ã§ã¯ãã®CGIã®URLã", :perms => "read" } cgi = CGI.new auth = Jugem::Auth.new(params) begin jugem = auth.login(cgi['frob'].to_s) # èªè¨¼ rescue Jugem::AuthError => e # èªè¨¼å¤±æ # auth.uri_to_login.to_s ã«ãªãã¤ã¬ã¯ã end
OpenID
Rubyã§OpenID
ruby-openidã©ã¤ãã©ãªã使ããOpenID2.0ã«ã対å¿ãã¦ããã®ã§ãYahoo!ã®ã§ã大ä¸å¤«ã
gem install ruby-openid
immediateã使ããªãå ´åã®ãµã³ãã«ãæ®éã¯ããã§ããã¨æãã¾ãã
Yahoo!ã§OpenIDã¤ãã£ã¦ãã²ã¨ã¯ãidentifierã¯ãyahoo.co.jpãã ãã§è¯ãã§ãã
#!/usr/bin/ruby require 'rubygems' require 'openid' require 'openid/store/filesystem' require 'cgi' require 'cgi/session' require 'cgi/session/pstore' @cgi = CGI.new session = CGI::Session.new(@cgi,'database_manager' => CGI::Session::PStore) def render(text) @cgi.out do text end end def redirect_to(url) print @cgi.header({'status' => '302 Found', 'Location' => url }) end store = OpenID::Store::Filesystem.new('éµäº¤æãã¼ã¿ã®ä¿åå ') consumer = OpenID::Consumer.new(session, store) if session[:verified_identifier] # ãã°ã¤ã³æ¸ã¿ render "#{session[:verified_identifier]}ã§ãã°ã¤ã³æ¸ã¿" else if !@cgi['openid.mode'].empty? #èªè¨¼çµæãã³ã¼ã«ããã¯ã§ããã£ã params = @cgi.params.clone params.each { |k, v| params[k] = v[0] } oid_res = consumer.complete(params, 'ã³ã¼ã«ããã¯URLï¼èªèº«ã®URLï¼') case oid_res.status when OpenID::Consumer::FAILURE # èªè¨¼ã«å¤±æãããã¡ãã»ã¼ã¸ãoid_res.messageã§åå¾å¯è½ã render oid_res.message when OpenID::Consumer::SUCCESS session[:verified_identifier] = oid_res.display_identifier redirect_to 'ãã°ã¤ã³å¾ã®ãã¼ã¸ã®URL' when OpenID::Consumer::CANCEL # èªè¨¼ãã¦ã¼ã¶ããã£ã³ã»ã«ãã render "ãã£ã³ã»ã«ããã¾ãã" end elsif !@cgi['openid_identifier'].empty? # ãã©ã¼ã ã«Identifierï¼URLï¼ãå ¥åãããã®ã§ãèªè¨¼å¦çãéå§ãã # @cgi['openid_identifier']ã§ã¯ä»¥ä¸ã®è¦åãåºã¦åããªãã # CAUTION! cgi['key'] == cgi.params['key'][0]; if want Array, use cgi.params['key'] identifier = @cgi.params['openid_identifier'][0] oid_req = consumer.begin(identifier) # ã³ã³ã·ã¥ã¼ãèå¥ç¨URLã¯ãããã¤ãã®èªè¨¼ç»é¢ã§è¡¨ç¤ºãããURLã£ã½ãã # 注æã¨ãã¦ã¯ãã³ã¼ã«ããã¯URLã¯ã³ã³ã·ã¥ã¼ãèå¥ç¨URLãé¨åä¸è´ããªããã°ãªããªããã¨ã redirect_to oid_req.redirect_url('ã³ã³ã·ã¥ã¼ãèå¥ç¨URL', 'ã³ã¼ã«ããã¯URL') else # ãã©ã¼ã 表示 render <<-FORM <h1>OpenID ãµã³ãã«</h1> <form method="post"> <input id="openid_identifier" name="openid_identifier" type="text" value="" /> <input name="commit" type="submit" value="èªè¨¼" /> </form> FORM end end
mixi
å°ãªãã¨ãä¸è¬å
¬éããã¦ãèªè¨¼APIã¯ãã¾ã®ã¨ãããªããããããªãMixiStationã®è¶³è·¡APIã®ããã¯ã¨ãªãããã
まちゅダイアリーã§ç´¹ä»ããã¦ããããã«ãã¦ã¼ã¶ã¼ããåãåã£ãã¢ã«ã¦ã³ãã¨ããã¹ã¯ã¼ãï¼ã®wsse:PasswordDigestï¼ã使ã£ã¦APIãå©ãã¦èªè¨¼ã«æåãããã©ãã確èªããã
Rubyã§mixièªè¨¼
//wsse.jsã使ããã¦ããã ãã //http://rvr.typepad.com/wind/2005/07/wsse_for_javasc.html function sendWSSE(form, requestURL) { var username = null; var password = null; var elements = form.getElements(); elements.each(function(element, index) { if(element.getAttribute('name') == 'email') username = element.value; if(element.getAttribute('name') == 'password') password = element.value; }); var wsseForm = document.getElementById('wsseForm') || document.createElement('form'); wsseForm.setAttribute('id', 'wsseForm'); wsseForm.action = requestURL; wsseForm.method = 'POST'; var wsseField = document.getElementById('wsseField') || document.createElement('input'); wsseField.setAttribute('id', 'wsseField'); wsseField.name = 'wsse'; wsseField.type = 'hidden'; wsseField.value = wsseHeader(username, password); document.body.appendChild(wsseForm); wsseForm.appendChild(wsseField); wsseForm.submit(); return false; }
<form onsubmit="return sendWSSE(this, 'mixi.cgi');"> mixi<br /> e-mail:<input type="text" name="email" value="" /><br /> password:<input type="password" name="password" value="" /><br /> <input type="submit" value="ãã°ã¤ã³" /> </form>
require 'cgi' require 'open-uri' def mixi_login(wsse) open('http://mixi.jp/atom/tracks', 'X-WSSE' => wsse) true rescue OpenURI::HTTPError => e false end @cgi = CGI.new mixi_login(@cgi['wsse']) # => èªè¨¼æåãããtrue