Ruby Open-Uri Redirect Forbidden

Ruby open-uri redirect forbidden

Have a look at the open_uri_redirections gem.

It patches Ruby's OpenURI to allow redirections from HTTP to HTTPS or the other way around.

open-uri is not redirecing http to https

See this Ruby bug report for a discussion of why you're experiencing this issue. See this gist for a monkey patch to OpenURI to allow "unsafe" redirects.

Ruby open-uri RuntimeError - redirection forbidden

Never mind, silly mistake. I put the begin..rescue block inside the Thread.do block and it worked.

Rails 3.2.17 Runtime Error Redirection Forbidden facebook


Update

If you are using omniauth-facebook please follow deivid's answer.

Another way to solve this issue is to replace http with https. In that way it will redirect from https to https and you won't get a redirection forbidden error.

Example

> url = auth.info.image
=> "http://graph.facebook.com/672086173/picture?type=square"

> avatar_url =url.gsub("­http","htt­ps")
=> "https://graph.facebook.com/672086173/picture?type=square"

I had the exact same problem. I solve it with following steps

First in your gemfile add

gem 'open_uri_redirections'

and run bundle install to install the gem

And then in your model

private

def process_uri(uri)
require 'open-uri'
require 'open_uri_redirections'
open(uri, :allow_redirections => :safe) do |r|
r.base_uri.to_s
end
end

Now process the avatar url with the method like

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

Hope this helps anyone else that may be having this issue.

redirection forbidden / bad uri (facebook) paperclip image

In your user model

#user.rb
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.create( image:process_uri(auth.info.image))
end

private

def self.process_uri(uri)
require 'open-uri'
require 'open_uri_redirections'
open(uri, :allow_redirections => :safe) do |r|
r.base_uri.to_s
end
end

Or if you don't want to use open uri redirections gem then change process_uri method to

def self.process_uri(uri)
avatar_url = URI.parse(uri)
avatar_url.scheme = 'https'
avatar_url.to_s
end

EU Central Bank Gem redirection forbidden

Apparently, you are facing the same problem as this one:

Ruby open-uri redirect forbidden

It's caused because the EU central bank forced a redirection to https but openuri library is not allowing this and that's why you get this error: redirection forbidden.

There is a gem called open_uri_redirections that will patch the openuri to allow redirections.

https://github.com/open-uri-redirections/open_uri_redirections

All you need to do is to:

  • include this gem in your Gemfile.
  • $ bundle install to install the newly added gem.
  • require 'open_uri_redirections'

This should fix your issue temporarily.


Another fix (recommended) is to monkey patch eu_central_bank gem to override

ECB_RATES_URL at eu_central_bank/lib/eu_central_bank.rb:20

ECB_90_DAY_URL at eu_central_bank/lib/eu_central_bank.rb:21

constants with https instead of http.

as follows (put this code to config/initializers/patch_eu_central_bank.rb):

require 'eu_central_bank'
class EuCentralBank
ECB_RATES_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'.freeze
ECB_90_DAY_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'.freeze
end

You might see some warnings like:

warning: already initialized constant EuCentralBank::ECB_RATES_URL.

You can live with it till the PR merged at the EuCentralBank gem.



Related Topics



Leave a reply



Submit