如何为图片屏蔽Facebook图形api网址? [英] How do I mask Facebook graph api URLs for pictures?

查看:161
本文介绍了如何为图片屏蔽Facebook图形api网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站上显示Facebook个人资料图片,但不想泄露源代码中人员的facebook ID。

I'm trying to display Facebook profile pictures on my site, but don't want to leak the facebook id's of the people in the source.

例如,此网址: http://graph.facebook.com/4/picture 将重定向到: http://profile.ak.fbcdn.net/hprofile-ak-snc4 /157340_4_3955636_q.jpg 在浏览器中加载它。我想获得第二个网址(CDN网址)并将其用作我的img src,因为它不会在网址中显示facebook ID。

For example, this URL: http://graph.facebook.com/4/picture will redirect to: http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg when you load it in a browser. I'd like to get the 2nd url (CDN url) and use it as my img src since it doesn't show the facebook id in the url.

我'我目前正在Ruby on Rails中这样做,我很好奇,如果有更好的方式,我在下面做了什么:

I'm doing this in Ruby on Rails at the moment and am curious if there's a better way that what I have done below:

def picture_square(facebook_id, secure=false)
  raw_url = "http://graph.facebook.com/" facebook_id + "/picture?type=square"
  if secure
    binary_img = ''
    open(raw_url) do |f|
      binary_img = f.read
    end
    encoded_img = Base64.encode64(binary_img)
    return 'data:image/jpg;base64,' + encoded_img.to_s
  else
    return raw_url
  end
end

你可以用以下HTML(使用上面的示例):

You could call this with the following HTML (using the above example):

<img src="<%= picture_square(4, true) %>"

这肯定有效并使用内联图像属性来实际渲染图像,但如果有点慢你有一堆你试图加载的图像。

This definitely works and uses the inline image properties to actually render the image, but it's a bit slow if you have a bunch of images that you're trying to load.

在Ruby中有没有办法让我可以获得重定向的URL并返回而不是尝试获取实际的原始二进制数据并将其编码为base64?

Is there a way in Ruby that I can get the redirected URL and just return that instead of trying to get the actual raw binary data and encode it to base64?

推荐答案

使用此url调用图API:
http://graph.facebook.com/4/?fields=picture& ; type = large

Make a call to the graph API with this url: http://graph.facebook.com/4/?fields=picture&type=large

这将返回您在json响应中寻找的图像。另一种选择是向您发布的第一个网址发出http请求,然后检查HTTP标头以读取位置标头..

This will return the image you are looking for inside the json response. The other option would be to make an http request to the first url you posted and then inspect the HTTP headers to read the location header..

这篇关于如何为图片屏蔽Facebook图形api网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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