2012-07-02 143 views
0

我有一個使用Facebook連接啓用用戶註冊/登錄(使用devise和omniauth)的rails應用程序。在閱讀Firesheep上的幾篇文章之後,我決定爲我的整個應用程序強制使用SSL通信。如何使用ssl測試本地主機上的Facebook-Connect

我跟着這個教程逼我的Rails應用程序3.0.x的只通過SSL進行通信:http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/

而且我跟着這個教程,這樣我可以測試應用程序在本地使用的WEBrick上的locahost:3001:http://blog.readypulse.com/2012/01/19/setup-webrick-to-serve-ssl-https-as-well-as-non-ssl-http-traffic-side-by-side/

到目前爲止好。我的應用在localhost:3000上運行時強制使用ssl通信。想如果我改變我的Facebook開發應用程序的網站的URL https://localhost:3001與Facebook進行通信連接

時出現我的問題,FB圖與 { "error": { "message": "Invalid redirect_uri: Given URL is not allowed by the Application configuration.", "type": "OAuthException", "code": 191 } }

響應但是,如果我更改了網站的網址,以http:localhost:3001我得到一個錯誤從我的服務器說,連接在加載頁面時被重置。我的瀏覽器URL欄顯示它試圖加載的網址是localhost:3001/auth/facebook/callback?code=MYCODEHERE,如果我只是在該網址前添加「https://」並重新加載,則頁面按預期加載。

也許我的問題源於我對SSL的基本知識。但我會不勝感激,如果有人可以向我解釋如何設置一個Facebook開發應用程序來支持我的本地測試通過ssl的Facebook連接?

回答

2

好的。得到它的工作。以下是我所做的,以便我可以通過localhost:3001上的ssl測試fb和twitter註冊/登錄。

首先,我將我的應用程序的FB站點url設置爲http://localhost:3001。然後我修改了omniauth初始化如下:

if RAILS_ENV == "production" 
    full_host = 'https://www.mydomain.com' 
    Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, 'myfbappid', 'myfbsecret', {:scope => 'email, publish_stream'} 
    provider :twitter, 'mytwitterappid', 'mytwittersecret' 
    end 
    Twitter.configure do |config| 
    config.consumer_key = 'myconsumerkey' 
    config.consumer_secret = 'myconsumersecret' 
    config.oauth_token = 'myoauthtoken' 
    config.oauth_token_secret = 'myoauthtokensecret' 
    end 
elsif RAILS_ENV == "development" 
    full_host = 'https://localhost:3001' 
    Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, 'myfbdevappid', 'myfbdefappsecret', {:scope => 'email, publish_stream'} 
    provider :twitter, 'mytwitterdevappid', 'mytwitterdevappsecret' 
    end 
    Twitter.configure do |config| 
    config.consumer_key = 'mytwitterconsumerkey' 
    config.consumer_secret = 'mytwitterconsumersecret' 
    config.oauth_token = 'mytwitteroauthtoken' 
    config.oauth_token_secret = 'mytwitteroathtokensecret' 
    end 
end 
OmniAuth.config.full_host = full_host 
+0

我需要使用'Rails.env ==「開發」'而不是'RAILS_ENV ==「development」'。 –

2

您需要設置一個指向您本地主機的子域(whatever IN A 127.0.0.1)或通過您的/etc/hosts文件創建所述子域。

在這兩種情況下,您可以使用http://yoursubdomain.yourdomain/auth/....yourdomain作爲Facebook應用註冊的域名。

+0

不知道我得到這個。我不使用任何子域。我只是希望FB圖使用https回撥。 –

+0

Facebook只允許與您爲應用註冊的網域相匹配的返回網址。因此,您需要爲其提供一個網址,該網址既可以是您在應用設置中輸入的域,也可以是其子域。由於你正在使用localhost,你需要一個DNS/hosts條目來給你一個這樣的子域名而不是'localhost'。 – ThiefMaster

+0

啊。我明白你的意思。但是,我走了一條不同的路線(我應該在第一篇文章中提到)。我有一個用於開發的FB應用程序(目前有一個http:// locahost:3000的站點地址)和一個用於生產的單獨的FB應用程序(指向www.myapp.com)。我的問題是,我無法讓FB開發應用程序回撥到「https:// localhost:3001」。它只想回撥到'http:// localhost:3001'。 –