如何为图片屏蔽Facebook图形api网址?

编程入门 行业动态 更新时间:2024-10-28 22:24:20
本文介绍了如何为图片屏蔽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.

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

For example, this URL: graph.facebook/4/picture will redirect to: profile.ak.fbcdn/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 = "graph.facebook/" 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: graph.facebook/4/?fields=picture& ; type = large

Make a call to the graph API with this url: graph.facebook/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网址?

本文发布于:2023-10-31 19:29:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   屏蔽   图形   网址   图片

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!