1

我使用Rails構建了一個應用程序,其中包含完全在localhost上工作的Facebook登錄功能,但現在它在Heroku上無法工作。看起來每個人都有一個共同的問題,但是過去的問題或其他文章都沒有幫助。Facebook登錄由omniauth在heroku上無法正常工作

error image

上述鏈路轉到誤差圖像。它應該來自Heroku但Facebook,因爲我在處理Stripe時看到了同樣的錯誤。在此錯誤開始困擾我之前,Facebook發現另一個錯誤Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings.,但它通過將Heroku URL添加到Facebook應用頁面來解決。

我做了figaro heroku:set -e production所以在Heroku中設置了應用程序密鑰和機密信息。

下面是我的文件中的一些代碼;

配置/初始化/ devise.rb

config.omniauth :facebook, ENV["facebook_app_id"], ENV["facebook_app_secret"], scope: 'email', info_fields: 'email,name', secure_image_url: true 

應用/模型/ user.rb

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
    user.email = auth.info.email 
    user.password = Devise.friendly_token[0,20] 
    user.name = auth.info.name # assuming the user model has a name 
    user.image = "http://graph.facebook.com/#{auth.uid}/picture?type=large" # assuming the user model has an image 
    # If you are using confirmable and the provider(s) you use validate emails, 
    # uncomment the line below to skip the confirmation emails. 
    # user.skip_confirmation! 
    end 
end 

控制器/用戶/ omniauth_callback_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    # You need to implement the method below in your model (e.g. app/models/user.rb) 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 

    def failure 
    redirect_to root_path 
    end 
end 

Heroku的日誌

2017-07-17T15:33:54.234171+00:00 app[web.1]: Started GET "https://stackoverflow.com/users/auth/facebook/callback?code=AQCoKbzr4 ///// 00703" for 150.116.22.144 at 2017-07-17 15:33:54 +0000 
2017-07-17T15:33:54.236011+00:00 app[web.1]: I, [2017-07-17T15:33:54.235951 #4] INFO -- omniauth: (facebook) Callback phase initiated. 
2017-07-17T15:33:54.360053+00:00 app[web.1]: Processing by Users::OmniauthCallbacksController#facebook as HTML 
2017-07-17T15:33:54.360097+00:00 app[web.1]: Parameters: {"code"=>"AQCoKbzr4nv6c7BEpM ///// 86c27a00703"} 
2017-07-17T15:33:54.371557+00:00 app[web.1]: User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "facebook"], ["uid", "102081518247"]] 
2017-07-17T15:33:54.581790+00:00 heroku[router]: at=info method=GET path="https://stackoverflow.com/users/auth/facebook/callback?code=AQCoK ///// a00703" host=xxxxxxx-xxxx-xxxxx.herokuapp.com request_id=93945-1199-417e-8d98-ede264cb fwd="150.116.22.144" dyno=web.1 connect=1ms service=350ms status=500 bytes=1754 protocol=https 
2017-07-17T15:33:54.578410+00:00 app[web.1]: Completed 500 Internal Server Error in 218ms (ActiveRecord: 3.0ms) 
2017-07-17T15:33:54.579175+00:00 app[web.1]: 
2017-07-17T15:33:54.579178+00:00 app[web.1]: RuntimeError (redirection forbidden: http://graph.facebook.com/102087018247/picture?type=large -> https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13064_10202475740292_410664266178542_n.jpg?oh=ef118e9d947604c9c7055a92e2&oe=5A02F8B4): 
2017-07-17T15:33:54.579178+00:00 app[web.1]: app/models/user.rb:18:in `block in from_omniauth' 
2017-07-17T15:33:54.579179+00:00 app[web.1]: app/models/user.rb:14:in `from_omniauth' 
2017-07-17T15:33:54.579180+00:00 app[web.1]: app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook' 
2017-07-17T15:33:54.579180+00:00 app[web.1]: 
2017-07-17T15:33:54.579181+00:00 app[web.1]: 

我不知道什麼RuntimeError從Heroku日誌表明...任何線索或建議,將不勝感激。

回答

0

你得到了重定向錯誤,因爲圖片url會將用戶重定向到另一個url。並且在將http重定向到https時在open-uri中存在限制。

在錯誤消息,你可以看到這個網址:通過在圖像URL

"https://graph.facebook.com/#{auth.uid}/picture?type=large" 

以https代替http或使用這種方式http://graph.facebook.com/102087018247/picture?type=large將被重定向到https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13064_10202475740292_410664266178542_n.jpg?oh=ef118e9d947604c9c7055a92e2&oe=5A02F8B4

可以解決這個問題:

user.remote_image_url = auth.info.image.gsub(/\Ahttp:/, "https") 
+0

加入工作!謝謝! – ymatt

+0

歡迎您:) ..可以標記答案爲正確的答案? –

+0

我剛剛點擊左側的檢查標誌。我是新來的,但希望這是標記的方式? – ymatt

0

請確保您已將Facebook開發者控制檯中的生產應用程序域列入白名單。

我通常會在我的默認應用程序中設置一個子測試應用程序,測試應用程序有自己的密鑰併爲它們設置ENV,並將本地主機列入白名單。這樣的開發更容易

然後在您的應用程序和Heroku中將生產應用程序的ENV設置爲Heroku,將Heroku域列入白名單。請確保您的回調包含Heroku的生產領域,匹配您列入白名單

一個

然後推到Heroku的後遷移的Heroku數據庫(這一個工程往往對我來說)

heroku run rake db:migrate 

者均基於這樣你的訪問圖像是不同的,我已經做到了。

user.remote_avatar_url = auth.info.image 

如果這不起作用,告訴我,我已經在我的時間在Heroku上設置了幾個Facebook登錄。

+0

其他答案看起來像最簡單的方式,但我很感謝您的建議! – ymatt