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