Ruby使用OAuth登录新浪微博和豆瓣
by
at 2011-01-09 12:49:03
original http://www.javaeye.com/topic/866280
首先需要安装oauth这个gem包
gem install oauth
新浪微博OAuth
申请新浪微博API key: http://open.t.sina.com.cn/wiki/index.php/%E6%96%B0%E6%89%8B%E6%8C%87%E5%8D%97
require 'rubygems' require 'oauth'your api key here
sina_api_key = ""
your api key secret here
sina_api_key_secret = ""
@consumer = OAuth::Consumer.new( sina_api_key, sina_api_key_secret, { :site=>"http://api.t.sina.com.cn", } )
1. get request_token
@request_token = @consumer.get_request_token
2. authorize & get oauth_verifier
puts "Copy this url to your browser to authorize, and get the oauth verifier code:" puts @request_token.authorize_url @oauth_verifier = "" # put the verfifier code here
3. get access_token
@access_token = @request_token.get_access_token(:oauth_verifier => @oauth_verifier)
4. API Example: get current user info
puts @access_token.get "/account/verify_credentials.xml"
API result:
<?xml version="1.0" encoding="UTF-8"?> <user> <id>1835404525</id> <screen_name>wendait</screen_name> <name>wendait</name> <province>11</province> <city>1000</city> <location>北京</location> <description>wenda.it - 做国内最好的IT专业知识问答网站</description> <url>http://1</url> <profile_image_url>http://tp2.sinaimg.cn/1835404525/50/1293540256/1</profile_image_url> <domain>plzdonttalkwithme</domain> <gender>m</gender> <followers_count>17</followers_count> <friends_count>103</friends_count> <statuses_count>307</statuses_count> <favourites_count>0</favourites_count> <created_at>Thu Oct 21 00:00:00 +0800 2010</created_at> <following>false</following> <verified>false</verified> <allow_all_act_msg>false</allow_all_act_msg> <geo_enabled>true</geo_enabled> <status> <created_at>Sun Jan 09 10:38:41 +0800 2011</created_at> <id>5087310493</id> <text>分享图片</text> <source> <a href="http://t.sina.com.cn/mobile/android.php">Android客户端</a> </source> <favorited>false</favorited> <truncated>false</truncated> <geo/> <in_reply_to_status_id></in_reply_to_status_id> <in_reply_to_user_id></in_reply_to_user_id> <in_reply_to_screen_name></in_reply_to_screen_name> <thumbnail_pic>http://ww1.sinaimg.cn/thumbnail/6d660cedjw6dd604n2phwj.jpg</thumbnail_pic> <bmiddle_pic>http://ww1.sinaimg.cn/bmiddle/6d660cedjw6dd604n2phwj.jpg</bmiddle_pic> <original_pic>http://ww1.sinaimg.cn/large/6d660cedjw6dd604n2phwj.jpg</original_pic> <annotations/> </status> </user>
豆瓣OAuth
申请豆瓣API key: http://www.douban.com/service/apikey/apply
require 'rubygems' require 'oauth'your api key here
douban_api_key = ""
your api key secret here
douban_api_key_secret = ""
@consumer = OAuth::Consumer.new( douban_api_key, douban_api_key_secret, { :site=>"http://www.douban.com", :request_token_path=>"/service/auth/request_token", :access_token_path=>"/service/auth/access_token", :authorize_path=>"/service/auth/authorize", :signature_method=>"HMAC-SHA1", :scheme=>:header, :realm=>"http://yoursite.com" } )
1. get request_token
@request_token = @consumer.get_request_token
2. authorize
puts "Copy this url to your browser to authorize:" puts @request_token.authorize_url
3. get access_token
@access_token = @request_token.get_access_token @access_token = OAuth::AccessToken.new( OAuth::Consumer.new( douban_api_key,
douban_api_key_secret, { :site=>"http://api.douban.com", :scheme=>:header, :signature_method=>"HMAC-SHA1", :realm=>"http://yoursite.com" } ), @access_token.token, @access_token.secret )4. API example: get current user info
puts @access_token.get("/people/%40me")
API result:
<?xml version="1.0" encoding="UTF-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:db="http://www.douban.com/xmlns/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:opensearch="http://a9.com/-/spec/opensearchrss/1.0/"> <id>http://api.douban.com/people/1398276</id> <title>hideto</title> <link href="http://api.douban.com/people/1398276" rel="self"/> <link href="http://www.douban.com/people/Hideto/" rel="alternate"/> <link href="http://img3.douban.com/icon/u1398276-1.jpg" rel="icon"/> <link href="http://hideto.javaeye.com" rel="homepage"/> <content>http://wenda.it</content> <db:attribute name="n_mails">0</db:attribute> <db:attribute name="n_notifications">1</db:attribute> <db:signature></db:signature> <db:uid>Hideto</db:uid> <uri>http://api.douban.com/people/1398276</uri> </entry>
<br><br>
作者: <a href="http://hideto.javaeye.com">hideto</a>
<br>
声明: 本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
<br><br>
<span style="color:red">
<a href="http://www.javaeye.com/topic/866280" style="color:red">已有 <strong>0</strong> 人发表回复,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
JavaEye推荐