OAuth方式登錄 - oauth_china

jopen 12年前發布 | 85K 次閱讀 OAuth OAuth開發包

簡介

  • 通過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


項目主頁:http://www.baiduhome.net/lib/view/home/1341721851244

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!