【问题标题】:Rails 3.2.17 Runtime Error Redirection Forbidden facebookRails 3.2.17 运行时错误重定向禁止
【发布时间】:2014-03-26 11:51:09
【问题描述】:

我有这段代码用于从 Facebook 获取头像...

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

当我现在尝试加载代码时,出现此错误:

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'

我知道这是 Open-URI 不允许 HTTP 到 HTTPS 重定向的问题...我知道这可以通过 Open-Uri-Redirections 插件https://github.com/jaimeiniesta/open_uri_redirections 解决

但是有两点我不明白:

  1. 昨天工作得很好……我什么也没做。那么,为什么 Paperclip 突然无法获取正确的 URL?
  2. Open-Uri-redirections 的说明给出了以下示例:

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

我将如何与上面的代码进行协调?

【问题讨论】:

  • 我也是同样的错误,昨天还在工作哈哈哈

标签: facebook redirect ruby-on-rails-3.2 open-uri


【解决方案1】:

我实际上认为最简洁的处理方式是直接通过https 请求头像。为此,只需使用

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

而不是

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

如果您使用omniauth-facebook,则需要在您的omniauth 初始化程序中指定secure_image_url: true 以生成该网址。像这样:

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

您的omniauth 初始化程序应该在您的config/initializers 目录中,如果您将它与devise 一起使用,可能称为omniauth.rbdevise.rb

【讨论】:

  • 确实是最干净的方式!非常感谢。
  • 如果每个人都在使用omniauth-facebook,这应该是公认的答案。谢谢。
  • 同意,这是一个更清晰的答案,并且就像一个魅力!感谢您建议使用omniauth-facebook 设置!
  • secure_image_url: true 是要走的路。
  • 你能详细说明一下这应该去哪里吗?
【解决方案2】:

更新

如果您使用的是omniauth-facebook,请按照 deivid 的回答。

解决此问题的另一种方法是将 http 替换为 https。这样,它将从 https 重定向到 https,并且您不会收到重定向禁止错误。

例子

> 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"

我遇到了完全相同的问题。我通过以下步骤解决它

首先在你的 gemfile 中添加

gem 'open_uri_redirections'

然后运行 ​​bundle install 来安装 gem

然后在你的模型中

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

现在用类似的方法处理头像url

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

希望这对可能遇到此问题的其他人有所帮助。

【讨论】:

  • 收到以下错误:身份验证中发生 Paperclip::AdapterRegistry::NoHandlerError #create:找不到“fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t5.0-1/…”回形针 (3.4.2) lib/paperclip/io_adapters/registry 的处理程序。 rb:19:in `handler_for'
  • 这一行应该是:user.update_attribute(:avatar, URI.parse(avatar_url)) 现在它可以工作了。 :)
  • “处理头像 url”方法去哪儿了...?它应该进入创建块吗?
  • @mysticcola 我也有类似的问题here
  • 出于某种原因,我不断收到 process_uri 的 NoMethodError 未定义方法,即使它显然在我的 User.rb 模型中。有什么想法吗?
【解决方案3】:

open_uri_redirections 对我不起作用。我可以通过将原始 facebook 图像 url 从 http 更改为 https 来使其工作。这样,在 https 上重定向到 akamai CDN 不是 http -> https 重定向,而是 https - https 重定向。

在你的例子中

user.update_attribute(:avatar, URI.parse(auth.info.image))

会变成

uri = URI.parse(auth.info.image)
uri.scheme = 'https'
user.update_attribute(:avatar, URI.parse(uri))

【讨论】:

    【解决方案4】:

    我遇到了同样的错误。昨天它正在工作。所以,我使用了以下没有 gem 的解决方案:

    url = URI.parse('<YOUR FACEBOOK URL>')
    
    h = Net::HTTP.new url.host, url.port
    h.use_ssl = url.scheme == 'https'
    
    head = h.start do |u|
      u.head url.path
    end
    
    new_url = head['location']
    

    希望对你有帮助。

    【讨论】:

      【解决方案5】:

      FWIW,@deep 的解决方案并不适合我,尽管它确实让我更加接近。

      我最终这样做了:

      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
      

      然后:

      avatar_url = process_uri(auth[:info][:image])
      new_user.update_attribute(:remote_avatar_url, avatar_url)
      

      【讨论】:

      • 许多人提到 Deep 的答案对他们不起作用......我无法完全弄清楚“为什么”......你得到的错误是什么?它第一次对我来说很完美......(嗯......第二次,在我修改了他的进程 uri 之后)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2020-03-27
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多