Rails 3.2.17 Runtime Error Redirection Forbidden facebook

mystic cola picture mystic cola · Mar 26, 2014 · Viewed 8k times · Source

I have this code I use to get avatars from Facebook...

if auth.info.image.present?
      user.update_attribute(:avatar, URI.parse(auth.info.image))
end

When I try to load the code now I get this error:

A RuntimeError occurred in authentications#create:

  redirection forbidden: http://graph.facebook.com/672086173/picture?type=square -> https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t5.0-1/1086349_672086173_156380036_q.jpg
  /home/ubuntu/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/open-uri.rb:223:in `open_loop'

I understand that this is a problem with Open-URI not allowing HTTP to HTTPS redirections... and I understand that this can be solved with Open-Uri-Redirections plugin https://github.com/jaimeiniesta/open_uri_redirections

But there are two things I don't understand:

  1. This was working just fine YESTERDAY... and I've changed nothing. So why, suddenly, can Paperclip not get the correct URL?
  2. The instructions for Open-Uri-redirections give the following example:

    open('http://github.com', :allow_redirections => :safe)

How would I reconcile this with my code above?

Answer

deivid picture deivid · Apr 16, 2014

I actually think the cleanest way of handling this is directly requesting the avatar through https. To do that, just use

https://graph.facebook.com/672086173/picture?type=square

instead of

http://graph.facebook.com/672086173/picture?type=square

If you're using omniauth-facebook, you'll need to specify secure_image_url: true in your omniauth initializer to generate that url. Like so:

config.omniauth :facebook, "XXXX", "XXXX",
                           image_size: { width: 500, height: 500 },
                           secure_image_url: true

Your omniauth initializer should be in your config/initializers directory, probably called omniauth.rb or devise.rb if you're using it together with devise.