2012-06-25 57 views
4

我已經在網上爲我的rails應用程序完美地運行了omniauth。我也爲我們的iPhone應用創建了一個API來進行交互,並且我試圖讓omniauth工作。Omniauth在Rails API中進行提供者身份驗證

有沒有辦法將訪問令牌(從集成的iOS集成與Facebook.app接收)傳遞給omniauth以在數據庫中創建提供程序條目?

現在在我的web應用程序,我有以下代碼

def create 
    omniauth = request.env["omniauth.auth"] 
    user = User.where("authentications.provider" => omniauth['provider'], "authentications.uid" => omniauth['uid']).first 

    if user 
     session[:user_id] = user.id 
     flash[:notice] = t(:signed_in) 
     redirect_to root_path 
    elsif current_user 
     user = User.find(current_user.id) 
     user.apply_omniauth(omniauth) 
     user.save 
     flash[:notice] = t(:success) 
     redirect_to root_path 
    else 
     session[:omniauth] = omniauth.except('extra') 
     flash[:notice] = "user not found, please signup, or login. Authorization will be applied to new account" 
     redirect_to register_path 
    end 
    end 

回答

3

在爲API我的用戶控制器的認證控制器我創建了以下內容:

def create 
    @user = User.new(params[:user]) 
    @user.save 

    # Generate data for omni auth if they're a facebook user 
    if params[:fb_access_token] 
     graph = Koala::Facebook::API.new(params[:fb_access_token]) 
     profile = graph.get_object('me') 

     @user['fb_id'] = profile['id'] 
     @user['fb_token'] = params[:fb_access_token] 
     @user['gender'] = profile['gender'] 

     # Generate omnihash 
     omnihash = Hash.new 
     omnihash['provider'] = 'facebook' 
     omnihash['uid'] = profile['id'] 

     omnihash['info'] = Hash.new 
     omnihash['info']['nickname'] = profile['username'] 
     omnihash['info']['name'] = profile['name'] 
     omnihash['info']['email'] = profile['email'] 
     omnihash['info']['first_name'] = profile['first_name'] 
     omnihash['info']['last_name'] = profile['last_name'] 
     omnihash['info']['verified'] = profile['verified'] 

     omnihash['info']['urls'] = Hash.new 
     omnihash['info']['urls']['Facebook'] = profile['link'] 

     omnihash['credentials'] = Hash.new 
     omnihash['credentials']['token'] = params[:fb_access_token] 

     omnihash['extra'] = Hash.new 
     omnihash['extra']['raw_info'] = Hash.new 

     puts omnihash 

     # Save the new data 
     @user.apply_omniauth(omnihash) 
     @user.save 
    end 
+0

如果你只是通過FB oauth標記原始的那樣,難道你不是在繞過整個oauth過程和假設的安全好處嗎? – freedrull

相關問題