ITeye博客

Ruby使用OAuth登录新浪微博和豆瓣

hideto · 2011-01-09 12:49 · Ruby · 编程语言 · Markdown

首先需要安装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.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>

评论(9)

  1. #9 楼 · zhangyuxiu · 2012-11-12 09:14

    实现使用ruby实现新浪oauth的代码中有:@oauth_verifier = "" # put the verfifier code here 请问这一句如何实现啊。验证码是在浏览器网页中的,要扣取出来么? 期待你的回复。谢谢!

  2. #8 楼 · mysoko · 2011-04-15 12:10

    怎么创建对应的站内帐户呢? 如果我想要email怎么办?

  3. #7 楼 · 易卡螺丝君 · 2011-01-30 16:00

    自己fork一下 不是很难吧

  4. #6 楼 · sevk · 2011-01-30 14:03

    给 oauth 打个 encode 补丁

  5. #5 楼 · bnulee · 2011-01-26 10:52

    这个问题有无解决啊?!!

  6. #4 楼 · playcase · 2011-01-19 09:32

    hideto writes:

    [quote=playcase]这个gem处理腾讯微薄不好用啊[/quote]

    腾讯的oauth api让人蛋疼,我试了下一直invalid signature错误,貌似是encode parameters方式的问题

    我测试也是401错误,invalid signature

    看了源码,发现这个gem里没有处理encode方式的参数。

  7. #3 楼 · hideto · 2011-01-18 14:55

    playcase writes:

    这个gem处理腾讯微薄不好用啊

    腾讯的oauth api让人蛋疼,我试了下一直invalid signature错误,貌似是encode parameters方式的问题

  8. #2 楼 · playcase · 2011-01-18 12:37

    这个gem处理腾讯微薄不好用啊

  9. #1 楼 · foohsinglong · 2011-01-16 14:52

    可以直接嵌入么?

评论功能维护中,当前仅展示历史评论。