想要将Facebook图像保存到我的Rails应用程序中 [英] Want to save facebook image into my rails app

查看:60
本文介绍了想要将Facebook图像保存到我的Rails应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ominauth在Rails应用程序中注册用户.我还有一个基本的注册选项,也包含电子邮件地址.因此,对于这两个用户,我都必须将上传用户图片保存在我的文件系统中,对于电子邮件用户,我要定期上传文件,但对于FB用户,我希望将其图片保存在我的光盘中,以便我可以使用相同的代码,而在不使用FB图形链接的情况下显示.

I am using ominauth to signup users in my rails app.i also have a basic signup option with email address also. So for both users i have to save upload user picture in my file system I have regular file upload for my email users but for FB users i want their pics to save in my disc so i can use same code and not use FB graph link while displaying.

FB使用图形API发送这种格式的图像

FB sends images in this format with graph API

http://graph.facebook.com/100007619644580/picture?type=large

如何将其保存在我存储所有用户图像的公用文件夹中.

How can i save that in my public folder where i store all the user images.

我尝试过

directory = "public/data/orig/"
      #name = num1+'_'+params[:upload]['datafile'].original_filename
      name = "new_name_for image"
      path = File.join(directory, name)
      #File.open(path, "wb") { |f| f.write(params[:upload_hover]['datafile'].read) }

      File.open(path, 'wb') do |file|
        file << open('http://graph.facebook.com/100007619644580/picture?type=large').read
      end

但这给了我错误

No such file or directory @ rb_sysopen - http://graph.facebook.com/100007619644580/picture?type=large

让我知道您是否还有其他解决方案.

Let me know if you have any other solutions.

推荐答案

谢谢您的回答,是的,我可以使用回形针gem,但是我已经在使用cloudinary上传图像,所以我不想使用其他基于图像的图像gem只是为了强制从fb用户配置文件中保存文件.我能够锻炼以下代码更改.

Thanks for the answer, Yes i could use the paperclip gem but i'm already using cloudinary to upload image so i didn't wanted to use another image based gem just to force save files from fb user profile. I was able to workout the following code changes.

omniauth.rb

:secure_image_url => true

使用带有:scope 的最后一个参数,该参数允许我并保护URL.

for last parameters with :scope which allowed me and secure URL.

并在我的身份验证控制器中

and in my auth controller

 img_path = env["omniauth.auth"]["info"]["image"].gsub(/$/,'?type=large')
    require "open-uri"

  num1 = 'logo_'+SecureRandom.hex(5)
  img_name = num1+'_fbimage.jpg'
  directory = "public/data/orig/org"
  path = File.join(directory, img_name)
  File.open(path, 'wb') do |file|
    file << open(img_path).read
  end  

需要"open-uri" :secure_image_url =>true 帮了我大忙.

这篇关于想要将Facebook图像保存到我的Rails应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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