[size=medium]首先需要安装oauth这个gem包[/size]
[code]
gem install oauth
[/code]
[size=medium]新浪微博OAuth[/size]
申请新浪微博API key: [url]http://open.t.sina.com.cn/wiki/index.php/%E6%96%B0%E6%89%8B%E6%8C%87%E5%8D%97[/url]
[code]
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>
[/code]
[size=medium]豆瓣OAuth[/size]
申请豆瓣API key: [url]http://www.douban.com/service/apikey/apply[/url]
[code]
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.iteye.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>
[/code]
[code]
gem install oauth
[/code]
[size=medium]新浪微博OAuth[/size]
申请新浪微博API key: [url]http://open.t.sina.com.cn/wiki/index.php/%E6%96%B0%E6%89%8B%E6%8C%87%E5%8D%97[/url]
[code]
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>
[/code]
[size=medium]豆瓣OAuth[/size]
申请豆瓣API key: [url]http://www.douban.com/service/apikey/apply[/url]
[code]
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.iteye.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>
[/code]