2012-07-31 47 views
0

我有以下問題:我的應用程序作爲Facebook應用程序運行,但在用戶第一次接受它的Facebook權限時,它被重定向到正確的URL,但在Facebook(畫布) 。我該如何解決它?Omniauth +設計重定向錯誤

這裏是我的實際設置:

devise.rb:config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'], scope: "email,publish_stream"

應用程序/控制器/ omniauth_callback_controller.rb

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 

     def passthru 
     render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false 
     end 


     def facebook 

     @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) 

     if @user.persisted? 
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" 

      sign_in_and_redirect @user, :event => :authentication 
     else 
      session["devise.facebook_data"] = request.env["omniauth.auth"] 
      redirect_to root_path 
     end 
     end 

     def after_sign_in_path_for(resource) 
     inicio_path 
     end 
    end 

的routes.rb

Pl::Application.routes.draw do 



     ActiveAdmin.routes(self) 

     mount Resque::Server, :at => "/resque" 

     devise_for :admin_users, ActiveAdmin::Devise.config 

     devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" } 

     devise_scope :user do 
     get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru' 
     end 

     match 'states' => 'game_plays#states' 
     match 'cities' => 'game_plays#cities' 
     match 'parties' => 'game_plays#parties' 
     match 'prefeito' => 'game_plays#prefeito' 
     match 'prefeitos' => 'game_plays#prefeitos' 
     match 'vereadores' => 'game_plays#vereadores' 
     match 'parties' => 'game_plays#parties' 
     match 'qualities' => 'game_plays#qualities' 
     match 'start' => 'game_plays#start' 
     match 'generated_image' => 'game_plays#generated_image' 
     match 'save_game_play' => 'game_plays#save_game_play' 
     match 'final' => 'game_plays#final', :as => :final 

     match 'inicio' => 'game_plays#index' 


     root :to => 'game_plays#bem_vindo' 

    end 

任何建議嗎?

回答

0

這是從我自己的經驗幾個月前,這個問題,我希望它可以幫助。

從Facebook的角度來看,這正是它應該做的。它不能識別請求來自何處的區別。您必須更改請求上的重定向網址,以使其保留在iframe中。

我發現要做到這一點的最簡單的方法是讓iframe url轉到my_website.com/fb-app,它設置了一個會話變量,讓您知道您在畫布中並確保它們已通過Facebook的。然後將它們重定向到正常情況下網站繼續運行的畫布網站,並且用戶從不通知任何內容。

def fbapp 

    session[:fbapp] = true 

    if user_signed_in? == false 
    redirect_to user_omniauth_authorize_path(:facebook) 
    else 
    redirect_to $APP_CANVAS_URL + "?auth_token=" + current_user.authentication_token 
    end 
end 

然後,在您的Application_controller.rb中,您需要更改重定向路徑。這是它在我的應用程序看起來像,注意$ APP_CANVAS_URL應該是沿東西線的https://apps.facebook.com/your_app_here

def after_sign_in_path_for(resource_or_scope) 
    if session[:fbapp] == true 
    $APP_CANVAS_URL + "?auth_token=" + current_user.authentication_token 
    else 
    root_path 
    end 
end 

我懷疑這是最好的方式,但它是我得到了什麼挫折小時後爲我工作。我希望它有幫助。