主题:OAuth gem for rails,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博
2011-08-23 23:55
459 查看
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代码
gem install oauth_china
使用
在Gemfile里添加:
Ruby代码
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"
演示
Ruby代码
#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接口新浪的是最方便的,无论文档还是认证流程。其他都是各种不按标准。。各种坑人啊。。尤其是搜狐网易。
地址:https://github.com/hooopo/oauth_china
目前完成oauth认证和发微薄功能,欢迎测试或者fork。
简介
引用OAuth gem for rails3,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博。
安装
Ruby代码
gem install oauth_china
使用
在Gemfile里添加:
Ruby代码
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"
演示
Ruby代码
#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,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博
- Orchard Oauth身份认证模块,在原基础上支持百度,豆瓣,开心,QQ,人人,新浪,淘宝
- 分享到新浪微博,腾讯微博,网易微博,开心,人人,豆瓣
- Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded
- Essential Studio for Windows Forms发布2017 v2,支持office 2016和主题定制
- rails 4 安装gem install mysql 报checking for main() in -llibmysql... no错
- 网易微博关闭 新浪、搜狐微博“退化”成测试版
- 分享到新浪微博,腾讯微博,网易微博,开心,人人,豆瓣
- 新浪,腾迅,网易微博OAuth统一认证接口实现
- 使用gem安装rails,付解决It seems your ruby installation is missing psych (for YAML output).
- Ruby on Rails,使用gem安装rails,付解决It seems your ruby installation is missing psych (for YAML output).
- 新浪、腾讯、人人、豆瓣、Facebook、Twitter OAuth认证实例 推荐
- 豆瓣出错Error generating final archive:Found duplicate file for APK:net/oauth/consumer.properties.sample
- 新浪微薄的挂件
- 边学边做ROR( 错误信息error_messages_for was removed from Rails and is now available as a plugin)
- ruby gem install rails 错误解决方法
- arcgis api for flex 高级主题(二) 自定义控件的开发
- ArcSDE10.5 For Oracle12.2的支持问题
- Berkeley DB for .NET发布了0.95新版,支持Berkeley DB 4.5
- 辽阳新浪微薄群