OAuth方式登录 - oauth_china
jopen
12年前
简介
- 通过OAuth方式同步用户消息到微博平台(支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博)
- 和omini-auth的区别:omini-auth是专门提供oauth授权和获取用户信息的gem(比如用新浪微博帐号登陆这种需求)
- oauth_china是一个方便的同步信息到其他微博平台的gem(用来做像follow5.com或http://fanfou.com/settings/sync这样需求)
#config/routes.rb match "syncs/:type/new" => "syncs#new", :as => :sync_new match "syncs/:type/callback" => "syncs#callback", :as => :sync_callback # encoding: UTF-8 class SyncsController < ApplicationController 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 root_path end private def build_oauth_token_key(name, oauth_token) [name, oauth_token].join("_") end end