Overcoming “Display forbidden by X-Frame-Options”

后端 未结 26 2319
梦谈多话
梦谈多话 2020-11-21 06:31

I\'m writing a tiny webpage whose purpose is to frame a few other pages, simply to consolidate them into a single browser window for ease of viewing. A few of the pages I\'

26条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 07:24

    The only question that has a bunch of answers. WElcome to the guide i wish i had when i was scrambling for this to make it work at 10:30 at night on the deadline day... FB does some weird things with canvas apps, and well, you've been warned. If youa re still here and you have a Rails app that will appear behind a Facebook Canvas, then you will need:

    Gemfile:

    gem "rack-facebook-signed-request", :git => 'git://github.com/cmer/rack-facebook-signed-request.git'
    

    config/facebook.yml

    facebook:
      key: "123123123123"
      secret: "123123123123123123secret12312"
    

    config/application.rb

    config.middleware.use Rack::Facebook::SignedRequest, app_id: "123123123123", secret: "123123123123123123secret12312", inject_facebook: false
    

    config/initializers/omniauth.rb

    OmniAuth.config.logger = Rails.logger
    SERVICES = YAML.load(File.open("#{::Rails.root}/config/oauth.yml").read)
    Rails.application.config.middleware.use OmniAuth::Builder do
      provider :facebook, SERVICES['facebook']['key'], SERVICES['facebook']['secret'], iframe:   true
    end
    

    application_controller.rb

    before_filter :add_xframe
    def add_xframe
      headers['X-Frame-Options'] = 'GOFORIT'
    end
    

    You need a controller to call from Facebook's canvas settings, i used /canvas/ and made the route go the main SiteController for this app:

    
    class SiteController < ApplicationController
      def index
        @user = User.new
      end
      def canvas
        redirect_to '/auth/failure' if request.params['error'] == 'access_denied'
        url = params['code'] ? "/auth/facebook?signed_request=#{params['signed_request']}&state=canvas" : "/login"
        redirect_to url
      end
      def login
      end
    end
    

    login.html.erb

    
    <% content_for :javascript do %>
      var oauth_url = 'https://www.facebook.com/dialog/oauth/';
      oauth_url += '?client_id=471466299609256';
      oauth_url += '&redirect_uri=' + encodeURIComponent('https://apps.facebook.com/wellbeingtracker/');
      oauth_url += '&scope=email,status_update,publish_stream';
    console.log(oauth_url);
      top.location.href = oauth_url;
    <% end %>
    

    Sources

    • The config i think came from omniauth's example.
    • The gem file (which is key!!!) came from: slideshare things i learned...
    • This stack question had the whole Xframe angle, so you'll get a blank space, if you don't put this header in the app controller.
    • And my man @rafmagana wrote this heroku guide, which now you can adopt for rails with this answer and the shoulders of giants in which you walk with.

提交回复
热议问题