[英]Facebook login by omniauth on heroku is not working
I built an app with Facebook login function by Rails which perfectly worked on localhost, but now it doesn't work on Heroku. 我通过Rails构建了一个具有Facebook登录功能的应用程序,该应用程序可以在localhost上完美运行,但是现在在Heroku上无法运行。 It looks like a common problem everyone gets, but none of the past questions or other articles helped.
看来每个人都会遇到一个常见问题,但过去的问题或其他文章都没有帮助。
The above link goes to the error image. 上面的链接转到错误图像。 It should be coming from Heroku but Facebook because I saw the same error when I was dealing with Stripe.
它应该来自Heroku,但来自Facebook,因为我在处理Stripe时遇到了相同的错误。 Before this error started bothering me, there was another error from Facebook saying
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.
在此错误开始困扰我之前,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.
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.
but it was solved by adding the Heroku url to the Facebook app page. 但是可以通过将Heroku网址添加到Facebook应用页面来解决。
I did figaro heroku:set -e production
so the app keys and secrets mush have been set in Heroku. 我做了
figaro heroku:set -e production
所以应用程序密钥和机密信息已在Heroku中设置。
Here are some codes from my files; 这是我文件中的一些代码;
config/initializers/devise.rb config / initializers / devise.rb
config.omniauth :facebook, ENV["facebook_app_id"], ENV["facebook_app_secret"], scope: 'email', info_fields: 'email,name', secure_image_url: true
app/models/user.rb app / models / 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
controllers/users/omniauth_callback_controller.rb 控制器/用户/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 logs heroku日志
2017-07-17T15:33:54.234171+00:00 app[web.1]: Started GET "/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="/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]:
I have no idea what RuntimeError
from the Heroku logs indicates... Any clue or advice would be appreciated. 我不知道Heroku日志中的
RuntimeError
表示什么...任何线索或建议,将不胜感激。
mMake sure you have whitelisted the production applications domain in Facebook Developers Console. m确保已在Facebook Developers Console中将生产应用程序域列入白名单。
I usually set up a sub test app from my default app, test app has its own keys and set up ENV for them and localhost is whitelisted. 我通常会从默认应用程序中设置一个子测试应用程序,该测试应用程序具有其自己的密钥,并为它们设置ENV,并且localhost被列入白名单。 This way development is easier
这样开发更容易
Then have ENV set for production app inside your App and Heroku, with Heroku domain whitelisted. 然后在您的应用程序和Heroku中为生产应用程序设置ENV,并将Heroku域列入白名单。 Make sure your callback contains the Heroku production domain, matching the one you whitelisted
确保您的回调包含Heroku生产域,与您列入白名单的域匹配
Then migrating Heroku database after the push to Heroku (this one works often for me) 然后在推送到Heroku后迁移Heroku数据库(这对我来说通常很有效)
heroku run rake db:migrate
Btw the way your accessing the image is different that how i've done it. 顺便说一句,您访问图像的方式与我的操作方式不同。
user.remote_avatar_url = auth.info.image
if this doesn't work, tell me, i've set up a few Facebook login on Heroku in my time. 如果这不起作用,请告诉我,我当时在Heroku上设置了一些Facebook登录名。
You got redirection error because the image url will redirect user to another url. 您收到重定向错误,因为图片网址会将用户重定向到另一个网址。 and there is a limitation in the open-uri when redirect http to https.
并且将http重定向到https时在open-uri中存在限制。
In the error message you can see this url: http://graph.facebook.com/102087018247/picture?type=large
will be redirected to https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13064_10202475740292_410664266178542_n.jpg?oh=ef118e9d947604c9c7055a92e2&oe=5A02F8B4
在错误消息中,您可以看到以下URL:
http://graph.facebook.com/102087018247/picture?type=large
: http://graph.facebook.com/102087018247/picture?type=large
type= 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
you can work around this issue by replacing http with https in your image url 您可以通过在图片网址中将HTTP替换为https来解决此问题
"https://graph.facebook.com/#{auth.uid}/picture?type=large"
or using this way: 或使用这种方式:
user.remote_image_url = auth.info.image.gsub(/\Ahttp:/, "https")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.