Javascript 从 facebook 图形 API 获取全尺寸图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27958438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:00:57  来源:igfitidea点击:

Get full sized picture from facebook graph API

javascriptregexfacebookfacebook-graph-apiredirect

提问by Simon Knittel

I'm using the graph api endpoint /PAGE_ID/poststo get all posts from a facebook page. Now I want the full sized image from these posts. The picture property of the returned objects gives me only a cropped version of that image.

我正在使用图形 API 端点/PAGE_ID/posts从 Facebook 页面获取所有帖子。现在我想要这些帖子中的全尺寸图片。返回对象的图片属性只给了我该图像的裁剪版本。

With the object id from these posts and the API endpoint /OBJECT_ID/pictureI get the only the small, normal and album sized version of the picture. But with a little modification to the URL I managed to get the full sized image.

使用这些帖子中的对象 ID 和 API 端点,/OBJECT_ID/picture我得到了图片的唯一小、正常和相册大小的版本。但是通过对 URL 稍加修改,我设法获得了完整尺寸的图像。

Example

例子

This URL:
https://graph.facebook.com/10152843929471041/picture

这个网址:https:
//graph.facebook.com/10152843929471041/picture

redirects to this URL:
https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/s720x720/10838228_10152843929471041_5251228402651650719_n.jpg

重定向到这个网址:https:
//fbcdn-sphotos-ba.akamaihd.net/hphotos-ak-xpa1/t31.0-8/s720x720/10838228_10152843929471041_5251228402619_n.jpg

I removed the 720x720from that URL to get this URL:
https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/s/10838228_10152843929471041_5251228402651650719_n.jpg

720x720从那个 URL 中删除了这个 URL:https:
//fbcdn-sphotos-ba.akamaihd.net/hphotos-ak-xpa1/t31.0-8/s/10838228_10152843929471041_525122840269165071

which is finally the full sized image.

这最终是全尺寸的图像。

I think, that I can achieve this modification with a regex pattern. But now is my question, how I can get the URL after the redirect from the original URL (the first one).

我认为,我可以使用正则表达式模式来实现这种修改。但现在是我的问题,如何从原始 URL(第一个)重定向后获取 URL。

Any ideas or simpler solutions?

任何想法或更简单的解决方案?

回答by luschn

This is how you can get larger pictures:

这是获得更大图片的方法:

/OBJECT-ID/picture?width=500&height=500

Or:

或者:

/OBJECT-ID/picture?type=large

Also take a look at answer in this thread: Facebook Graph API : get larger pictures in one request

另请查看此线程中的答案:Facebook Graph API:在一个请求中获取更大的图片

Edit: As this does not seem to work with Object IDs, you can just grab the image from this response:

编辑:由于这似乎不适用于对象 ID,您可以从该响应中获取图像:

https://graph.facebook.com/10152843929471041

Look out for the "images" array.

注意“图像”数组。

回答by turdus-merula

One can also request the imagescollection of a photoobject, then search for the highest resolution entry.

还可以请求对象的images集合photo,然后搜索最高分辨率的条目

See documentation. Code:

请参阅文档。代码:

MyFacebookWrapper.getBestImage = function(photoId) {
    var deferred = new $.Deferred();
    var params = { fields: "images" };

    FB.api("/" + photoId, "get", params,
        function (response) {
            console.log("MyFacebookWrapper.getBestImage, response:");
            console.log(response);

            var images = _.sortBy(response.images, 'width');
            var best = _.last(images)

            deferred.resolve(best); 
        }
    );

    return deferred.promise();
};

Usage:

用法:

MyFacebookWrapper.getBestImage("photo Id ...").then(function(image) {
    console.log(image);
});