禁止重定向/坏 uri (facebook) 回形针图像 [英] redirection forbidden / bad uri (facebook) paperclip image

查看:42
本文介绍了禁止重定向/坏 uri (facebook) 回形针图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户的 Facebook 网址保存到我的数据库中,但出现以下错误.

I'm trying to save a users facebook url into my db, but I get the following error.

redirection forbidden: http://graph.facebook.com/1240771104/picture -> https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t...

我参考了这个问题,但是我认为他只是调用图像而不是将其保存到带有创建块的数据库中.

I've referenced this question but I think he is just calling an image instead of saving it to a db with a create block.

这是我的用户模型...

Here is my user model...

def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
    user = User.where(:provider => auth.provider, :uid => auth.uid).first
    if user
      return user
    else
      registered_user = User.where(:email => auth.info.email).first
      if registered_user
        return registered_user
      else
        user = User.create( 
                            name:auth.extra.raw_info.name,
                            provider:auth.provider,
                            uid:auth.uid,
                            email:auth.info.email,
                            image:auth.info.image,
                            password:Devise.friendly_token[0,20],
                          )
      end    
    end
end

我在def.self_for_facebook"下面添加了一个 URI 助手,因为我认为我只需要将 http 获取到 https.所以我从另一个堆栈问题中收集.

I've added a URI helper below the 'def.self_for_facebook' because I think I just need to get the http to https. So I gathered from the other stack question.

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

我需要添加吗

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

到创建块?这个:

        if auth.info.image.present?
            uri = URI.parse(auth.info.image)
            uri.scheme = 'https'
            user.update_attribute(:image, URI.parse(uri))
        end

        user = User.create( 
                            name:auth.extra.raw_info.name,
                            provider:auth.provider,
                            uid:auth.uid,
                            email:auth.info.email,
                            password:Devise.friendly_token[0,20],
                          )

      end    

让我...

bad URI(is not URI?): https://graph.facebook.com/1240771104/picture

但是那是怎么回事.链接给我图片!!哇,伙计!我一定离得很近.

But what's up with that. The link gets me the picture!! Aww man! I must be so close.

推荐答案

在你的用户模型中

#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

或者如果你不想使用 open uri redirections gem 然后将 process_uri 方法更改为

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

这篇关于禁止重定向/坏 uri (facebook) 回形针图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆