0

我有以下问题:我的应用程序作为 Facebook 应用程序运行,但在用户第一次接受它的 Facebook 权限时,它被重定向到正确的 URL,但在 Facebook(画布)之外。我该如何解决?

这是我的实际设置:

设计.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

路线.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

有什么建议吗?

4

1 回答 1

0

这是我几个月前在这个问题上的经验,我希望它有所帮助。

从 Facebook 的角度来看,这正是它应该做的。它无法识别请求来源之间的差异。您必须更改请求中的重定向 URL 以使其保留在 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

我怀疑这是最好的方法,但这是我经过数小时的挫折后必须为我工作的方法。我希望它有所帮助。

于 2012-08-13T20:43:32.850 回答