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

http://www.iteye.com/topic/975805
http://www.iteye.com/topic/866280

地址:https://github.com/hooopo/oauth_china
目前完成oauth认证和发微薄功能,欢迎测试或者fork。

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

安装
Ruby代码  收藏代码
  1. gem install oauth_china  

使用
在Gemfile里添加:

Ruby代码  收藏代码
  1. gem 'oauth'   
  2. 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"

演示

 
Ruby代码  收藏代码
  1. #config/oauth/sina.yml  
  2.   development:  
  3.         key:    "you api key"  
  4.         secret: "your secret"  
  5.         url:    "http://yoursite.com"  
  6.         callback: "http://localhost:3000/syncs/sina/callback"  
  7.       production:  
  8.         key:    "you api key"  
  9.         secret: "your secret"  
  10.         url:    "http://yoursite.com"  
  11.         callback: "http://localhost:3000/syncs/sina/callback"  
  12.   
  13.   
  14.   #config/routes.rb  
  15.   match "syncs/:type/new" => "syncs#new":as => :sync_new  
  16.   match "syncs/:type/callback" => "syncs#callback":as => :sync_callback  
  17.   
  18.   #app/controllers/syncs_controller.rb  
  19.   # encoding: UTF-8  
  20.   class SyncsController < ApplicationController  
  21.   
  22.     before_filter :login_required  
  23.   
  24.     def new  
  25.       client = OauthChina::Sina.new  
  26.       authorize_url = client.authorize_url  
  27.       Rails.cache.write(build_oauth_token_key(client.name, client.oauth_token), client.dump)  
  28.       redirect_to authorize_url  
  29.     end  
  30.   
  31.     def callback  
  32.       client = OauthChina::Sina.load(Rails.cache.read(build_oauth_token_key(params[:type], params[:oauth_token])))  
  33.       client.authorize(:oauth_verifier => params[:oauth_verifier])  
  34.   
  35.       results = client.dump  
  36.   
  37.       if results[:access_token] && results[:access_token_secret]  
  38.         #在这里把access token and access token secret存到db  
  39.         #下次使用的时候:  
  40.         #client = OauthChina::Sina.load(:access_token => "xx", :access_token_secret => "xxx")  
  41.         #client.add_status("同步到新浪微薄..")  
  42.         flash[:notice] = "授权成功!"  
  43.       else  
  44.         flash[:notice] = "授权失败!"  
  45.       end  
  46.       redirect_to account_syncs_path  
  47.     end  
  48.   
  49.     private  
  50.     def build_oauth_token_key(name, oauth_token)  
  51.       [name, oauth_token].join("_")  
  52.     end  
  53.   
  54.   end  

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

ps.抱怨一下,国内这些开放api接口新浪的是最方便的,无论文档还是认证流程。其他都是各种不按标准。。各种坑人啊。。尤其是搜狐网易。

原文地址:https://www.cnblogs.com/lexus/p/2151409.html