通过Graph API轻松访问Facebook头像
我正在使用Facebook的图形API。
我想下载我所有用户的Facebook头像的高清图片。
https://graph.facebook.com/<用户别名>/picture
这个链接可以获取到用户当前头像的一个小缩略图。
如果我想下载用户的高清头像,看起来我需要做一些类似于下面这个伪代码的操作...
# Get albums
albums = fetch_json('https://graph.facebook.com/<user alias>/albums')
# Get profile pictures album
profile_picture_album = albums['data']['Profile Pictures'] # Get profile picture album
# Get the pictures from that album
profile_pictures = fetch_json('https://graph.facebook.com/<profile_picture_album_id>/photos')
# Get the most recent (and therefore current) profile picture
current_profile_picture = profile_pictures['data'][0]
image = fetch_image_data(current_profile_picture['source'])
问题是,这需要两次不同的API访问,然后才能下载图片。如果有很多相册或者一个相册里有很多图片,那我还得处理分页的问题。
感觉应该有更快、更简单的方法来获取用户当前的头像。有没有人知道呢?
(顺便说一下:我正在用Python来做这件事,但我想答案应该和编程语言无关)
2 个回答
这段话展示了用户头像的原始大图:
https://graph.facebook.com/someuser/picture?width=9999&height=9999
我觉得你可能不能一步到位,但你有几个选择:
1.
你可以在获取照片时指定一个类型参数为 large
(不过这样只能得到最大200像素的图片):
http://graph.facebook.com/UID/picture?type=large
2.
你可以直接获取个人资料相册中的封面照片——这张照片总是当前的个人资料照片:
https://graph.facebook.com/UID/albums?access_token=TOKEN
这会返回类似于以下内容:
{
"id": "123456781234",
"from": {
"name": "FirstName Surname",
"id": "123456789"
},
"name": "Profile Pictures",
"link": "http://www.facebook.com/album.php?aid=123456&id=123456789",
"cover_photo": "12345678912345123",
"privacy": "friends",
"count": 12,
"type": "profile",
"created_time": "2000-01-23T23:38:14+0000",
"updated_time": "2011-06-15T21:45:14+0000"
},
然后你可以访问:
https://graph.facebook.com/12345678912345123?access_token=TOKEN
并选择一个图片大小:
{
"id": "12345678912345123",
"from": {
"name": "FirstName Surname",
"id": "123456789"
},
"name": "A Caption",
"picture": "PICTUREURL",
"source": "PICTURE_SRC_URL",
"height": 480,
"width": 720,
"images": [
{
"height": 608,
"width": 912,
"source": "PICTUREURL"
},
{
"height": 480,
"width": 720,
"source": "PICTUREURL"
},
{
"height": 120,
"width": 180,
"source": "PICTUREURL"
},
{
"height": 86,
"width": 130,
"source": "PICTUREURL"
},
{
"height": 50,
"width": 75,
"source": "PICTUREURL"
}
],
"link": "FACEBOOK_LINK_URL",
"icon": "FACEBOOK_ICON_URL",
"created_time": "2000-01-15T08:42:42+0000",
"position": 1,
"updated_time": "2011-06-15T21:44:47+0000"
}
然后选择你想要的 PICTUREURL
。
3.
感谢这篇博客:
//get the current user id
FB.api('/me', function (response) {
// the FQL query: Get the link of the image, that is the first in the album "Profile pictures" of this user.
var query = FB.Data.query('select src_big from photo where pid in (select cover_pid from album where owner={0} and name="Profile Pictures")', response.id);
query.wait(function (rows) {
//the image link
image = rows[0].src_big;
});
});
我并不想为引用这段内容而自夸,但我在玩测试样本时也想出了基本相同的FQL查询。当我搜索 FB.Data.query
时,这位朋友比我早一步。我想如果你想用Python来实现,你可能需要把它改成Python格式,如果需要的话我可以帮你找找。