OAuth gem for rails,支持豆瓣,新浪菲薄单薄,腾讯微博,搜狐微博,网易微博

OAuth gem for rails,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博
地址:https://github.com/hooopo/oauth_china
目前完成oauth认证和发微薄功能,欢迎测试或者fork。

简介
引用
OAuth gem for rails3,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博。

安装
gem install oauth_china

使用
在Gemfile里添加:

gem 'oauth' 
gem 'oauth_china'


添加配置文件

配置文件路径:
引用
config/oauth/douban.yml
config/oauth/sina.yml
config/oauth/qq.yml
config/oauth/sohu.yml
config/oauth/netease.yml


配置文件格式:
引用
development:
   key:    "you api key"
   secret: "your secret"
   url:    "http://yoursite.com"
   callback: "http://localhost:3000/your_callback_url"
production:
   key:    "you api key"
   secret: "your secret"
   url:    "http://yoursite.com"
   callback: "http://localhost:3000/your_callback_url"

演示

 
   #config/oauth/sina.yml
     development:
           key:    "you api key"
           secret: "your secret"
           url:    "http://yoursite.com"
           callback: "http://localhost:3000/syncs/sina/callback"
         production:
           key:    "you api key"
           secret: "your secret"
           url:    "http://yoursite.com"
           callback: "http://localhost:3000/syncs/sina/callback"


     #config/routes.rb
     match "syncs/:type/new" => "syncs#new", :as => :sync_new
     match "syncs/:type/callback" => "syncs#callback", :as => :sync_callback

     #app/controllers/syncs_controller.rb
     # encoding: UTF-8
     class SyncsController < ApplicationController

       before_filter :login_required

       def new
         client = OauthChina::Sina.new
         authorize_url = client.authorize_url
         Rails.cache.write(build_oauth_token_key(client.name, client.oauth_token), client.dump)
         redirect_to authorize_url
       end

       def callback
         client = OauthChina::Sina.load(Rails.cache.read(build_oauth_token_key(params[:type], params[:oauth_token])))
         client.authorize(:oauth_verifier => params[:oauth_verifier])

         results = client.dump

         if results[:access_token] && results[:access_token_secret]
           #在这里把access token and access token secret存到db
           #下次使用的时候:
           #client = OauthChina::Sina.load(:access_token => "xx", :access_token_secret => "xxx")
           #client.add_status("同步到新浪微薄..")
           flash[:notice] = "授权成功!"
         else
           flash[:notice] = "授权失败!"
         end
         redirect_to account_syncs_path
       end

       private
       def build_oauth_token_key(name, oauth_token)
         [name, oauth_token].join("_")
       end

     end

注意
系统时间要正确设置。否则会出现timstamps refused错误

ps.抱怨一下,国内这些开放api接口新浪的是最方便的,无论文档还是认证流程。其他都是各种不按标准。。各种坑人啊。。尤其是搜狐网易。OAuth gem for rails,支持豆瓣,新浪菲薄单薄,腾讯微博,搜狐微博,网易微博
1 楼 elementstorm 2011-03-26  
虎跑V5啊,我看好你哦~~~
2 楼 caryl 2011-03-26  
支持,感谢hooopo的辛勤劳动。
3 楼 jinleileiking 2011-03-28  
看着hoooooooop 成长
4 楼 liltos 2011-03-28  
此前也整了个类似的
不过我觉得腾讯更坑爹。。。
5 楼 zires 2011-03-29  
也准备写个的,一直没时间
gem名字都一样
6 楼 mewleo 2011-03-30  
好插件,啥时候加入读微薄功能。
7 楼 wowpzp 2011-04-05  
OAuth gem for rails,支持豆瓣,新浪菲薄单薄,腾讯微博,搜狐微博,网易微博 感谢楼主,项目刚好用上
8 楼 Hooopo 2011-05-17  
更新了一下,支持上传图片了(现在只有新浪可以)
9 楼 lemonweirui 2011-05-17  
为啥豆瓣的oauth在成功获得request_token之后,换取access_token的时候oauth抛出错误啊。桌面应用不需要提供callback参数吧。

10 楼 Hooopo 2011-05-18  
图片上传支持新浪和搜狐还有163微博了。
腾讯的上传图片总返回:"{\"data\":null,\"msg\":\"file size error\",\"ret\":1}"

同样的代码在新浪和搜狐就ok。
网易的发图片API接口真变态,分步骤:
引用

注意:本接口只是传图,并不能发微博,如果发带图片的微博,请先调用本接口得到upload_image_url后,再调用statuses/update,并将upload_image_url作为作为status参数值

http://open.t.163.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87(statuses/upload)
11 楼 Hooopo 2011-05-18  
lemonweirui 写道
为啥豆瓣的oauth在成功获得request_token之后,换取access_token的时候oauth抛出错误啊。桌面应用不需要提供callback参数吧。


这得看豆瓣的API文档要求了。
12 楼 kingze 2011-05-27  
我想弱弱地问一下,如何能取得他的用户名和 ID号呢???
13 楼 wowpzp 2011-05-27  
kingze 写道
我想弱弱地问一下,如何能取得他的用户名和 ID号呢???


以新浪为例
      resp = client.get '/account/verify_credentials.json'
      sina_json_data = resp.body
      sina_hash = ActiveSupport::JSON.decode(sina_json_data)

      sina_id = sina_hash['id']
      sina_name = sina_hash['name']

OAuth gem for rails,支持豆瓣,新浪菲薄单薄,腾讯微博,搜狐微博,网易微博
14 楼 struts 2012-04-26  
好东西,正好项目中用到OAuth gem for rails,支持豆瓣,新浪菲薄单薄,腾讯微博,搜狐微博,网易微博
15 楼 fvb7811032 2012-06-28  
樓主,我想問下,我騰訊微博的圖片文字分享已經實現,就是子啊做SINA的時候,因為用的都是Oauth 騰訊微博的URL我是這樣寫的String url ="http://open.weibo.com/authentication?oauth_token="+ oauth_token;,新浪微博該怎麼寫啊,求教
16 楼 Hooopo 2012-06-28  
fvb7811032 写道
樓主,我想問下,我騰訊微博的圖片文字分享已經實現,就是子啊做SINA的時候,因為用的都是Oauth 騰訊微博的URL我是這樣寫的String url ="http://open.weibo.com/authentication?oauth_token="+ oauth_token;,新浪微博該怎麼寫啊,求教

不会java..
17 楼 fvb7811032 2012-06-28  
樓主,我想問下,我騰訊微博的圖片文字分享已經實現,就是子啊做SINA的時候,因為用的都是Oauth 騰訊微博的URL我是這樣寫的String url ="http://open.weibo.com/authentication?oauth_token="+ oauth_token;,新浪微博該怎麼寫啊,求教   我也是android的,
18 楼 fvb7811032 2012-06-28  
Hooopo 写道
fvb7811032 写道
樓主,我想問下,我騰訊微博的圖片文字分享已經實現,就是子啊做SINA的時候,因為用的都是Oauth 騰訊微博的URL我是這樣寫的String url ="http://open.weibo.com/authentication?oauth_token="+ oauth_token;,新浪微博該怎麼寫啊,求教

不会java..

安卓的
19 楼 Hooopo 2012-06-28  
fvb7811032 写道
Hooopo 写道
fvb7811032 写道
樓主,我想問下,我騰訊微博的圖片文字分享已經實現,就是子啊做SINA的時候,因為用的都是Oauth 騰訊微博的URL我是這樣寫的String url ="http://open.weibo.com/authentication?oauth_token="+ oauth_token;,新浪微博該怎麼寫啊,求教

不会java..

安卓的

更不会了OAuth gem for rails,支持豆瓣,新浪菲薄单薄,腾讯微博,搜狐微博,网易微博