获得“真实”来自图形API的Facebook个人资料图片URL [英] Getting the "real" Facebook profile picture URL from graph API
问题描述
Facebook graph API告诉我,我可以使用
http://graph.facebook.com/517267866/picture?type=large
哪些作品精细。但是,当您在浏览器中输入以上URL时,图像的实际地址为
http://profile.ak.fbcdn.net/profile-ak-snc1/v227/560/83/n517267866_1928.jpg
如何以编程方式获取第一个URL?
第一个URL给出了第二个HTTP 302(临时重定向)。因此,要以第二个URL方式查找第二个URL,您可以为第一个URL发出HTTP请求,并获取响应的位置
头。
也就是说,不要依赖第二个URL。阅读一下HTTP响应代码(相对于永久性301的HTTP响应代码),Facebook可以定期更改这些URL,以防止人们使用,例如使用他们的服务器托管图像。
编辑:请注意,OP发布的CDN URL现在是404,所以我们知道我们不能依靠URL长期存在。此外,如果您在SSL保护页面上的< img>
链接到Graph API,则 有一个参数,你必须添加,确保你使用 http s ://graph.facebook .com
。
更新: API有添加了一个参数– redirect = false
–这导致返回JSON而不是重定向。重新运行的JSON包含CDN网址:
{
pre>
data:{
url http://profile.ak.fbcdn.net / ...,
is_silhouette:false
}
}
再次,我不会依赖这个CDN网址长期存在。 JSON响应与允许的CORS头文件一起发送,因此您可以随意使用XHR请求进行此客户端。
Facebook graph API tells me I can get a profile picture of a user using
http://graph.facebook.com/517267866/picture?type=large
which works fine. However, when you type above URL into a browser, the actual address of the image is
http://profile.ak.fbcdn.net/profile-ak-snc1/v227/560/83/n517267866_1928.jpg
How can I get the second URL using the first one programmatically?
解决方案The first URL gives a HTTP 302 (temporary redirect) to the second. So, to find the second URL programatically, you could issue a HTTP request for the first URL and get the
Location
header of the response.That said, don't rely on the second URL being pemanent. Reading a little in to the HTTP response code (of 302 as opposed to a permanent 301), it is possible Facebook changes those URLs on a regular basis to prevent people from—for example—using their servers to host images.
Edit: Notice that the CDN URL the OP posted is now a 404, so we know that we cannot rely on the URL being long-lived. Also, if you're linking to the Graph API from an
<img>
on a SSL-secured page,there's a parameter you have to addmake sure you usehttps://graph.facebook.com
.
Update: The API has added a parameter –
redirect=false
– which causes JSON to be returned rather than a redirect. The retruned JSON includes the CDN URL:{ "data": { "url": "http://profile.ak.fbcdn.net/...", "is_silhouette": false } }
Again, I wouldn't rely on this CDN URL being long-lived. The JSON response is sent with permissive CORS headers, so you're free to do this client-side with XHR requests.
这篇关于获得“真实”来自图形API的Facebook个人资料图片URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!