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

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

[b]简介[/b]
[quote]OAuth gem for rails3,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博。[/quote]
[b]安装[/b]
gem install oauth_china

[b]使用[/b]
在Gemfile里添加:

gem 'oauth' 
gem 'oauth_china'


添加配置文件

配置文件路径:
[quote]config/oauth/douban.yml
config/oauth/sina.yml
config/oauth/qq.yml
config/oauth/sohu.yml
config/oauth/netease.yml[/quote]

配置文件格式:
[quote] 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"[/quote]
[b]演示[/b]

   #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

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

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

https://db-china.org
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值