简体   繁体   English

如何通过一次调用从Facebook图形API获取给定URL的图像URL

[英]How to get an image url from Facebook graph API for a given URL in a single call

I am trying to get the image url data from a shared link in Facebook graph API. 我正在尝试从Facebook图形API中的共享链接获取图像URL数据。 I can get this but it seems a very hacky way to do so as it involves a double server call and relies on an old API version and I would much rather a nice clean way to do this with current version of the API - 2.5. 我可以做到这一点,但是这样做似乎很麻烦,因为它涉及到两次服务器调用,并且依赖于旧的API版本,而我更希望使用当前版本的API(2.5版)来完成此操作。

Up until 2.4 I could use the 2.3 API to request the below with the url of the link I am trying to get an image for https://graph.facebook.com/v2.3/?id=http%3A%2F%2Fwww.google.co.uk&access_token=xxxxx 直到2.4为止,我可以使用2.3 API来请求下面的链接的URL,我正在尝试获取https://graph.facebook.com/v2.3/?id=http%3A%2F%2Fwww.google.co.uk&access_token=xxxxx的图片https://graph.facebook.com/v2.3/?id=http%3A%2F%2Fwww.google.co.uk&access_token=xxxxx

and it would return the same as the 2.5 version of the API: 它将返回与API 2.5版本相同的代码:

{
   "og_object": {
      "id": "383886459583",
      "description": "Happy St. David's Day 2016 #GoogleDoodle",
      "title": "Google",
      "type": "website",
      "updated_time": "2016-03-01T10:58:02+0000",
      "url": "http://www.google.co.uk/"
   },
   "share": {
      "comment_count": 0,
      "share_count": 329164
   },
   "id": "http://www.google.co.uk"
}

From this I could take the og_object ID and make the exact same call but using the ID this time instead to return scrape information containing the image url: 由此,我可以使用og_object ID并进行完全相同的调用,但是这次使用该ID来返回包含图像URL的抓取信息:

https://graph.facebook.com/v2.3/?id=383886459583&access_token=xxxxx

{
   "created_time": "2007-11-07T12:09:31+0000",
   "title": "Google",
   "type": "website",
   "description": "Happy St. David's Day 2016 #GoogleDoodle",
   "image": [
      {
         "height": 210,
         "url": "http://www.google.com/logos/doodles/2016/st-davids-day-2016-5676738174517248-thp.png",
         "width": 525
      }
   ],
   "is_scraped": true,
   "updated_time": "2016-03-01T10:58:02+0000",
   "url": "http://www.google.co.uk/",
   "id": "383886459583"
}

and from there I had an image url I could use however using the same method in 2.5 to get the image yields this 从那里我有一个图片网址,我可以使用,但是在2.5中使用相同的方法来获取图片

{
   "created_time": "2007-11-07T12:09:31+0000",
   "title": "Google",
   "type": "website",
   "id": "383886459583"
}

Is there a way to get the image data in 2.5 using the ID or better still a way in which I can make a single call using the URL of the target link and return all of the og data at once? 有没有一种方法可以使用ID或以更好的方式获取2.5中的图像数据,还是可以使用目标链接的URL进行一次调用并立即返回所有og数据的方法?

Example of this in action would be 这方面的例子是

var url = "https://graph.facebook.com/v2.3/{{{{id}}}}?access_token={{{{token}}}}";
var token = "xxxxx";
var id = "http%3A%2F%2Fwww.google.co.uk";
url = url.replace('{{{{token}}}}', token);

$.getJSON( url.replace('{{{{id}}}}', id), 
  function( data ) {
    console.log(data);
    if (data && data.og_object && data.og_object.id)
      $.getJSON( url.replace('{{{{id}}}}', data.og_object.id), 
        function( data ){
          console.log(data, data.image);
          //Do something with og data
        }
      );
  }
);

EDIT: Thanks for pointing out the declarative fields CBroe ( I updated the title to better reflect my desired answer ) but the duplicate you marked doesnt mention nested fields which would be necessary to fully answer this and limit this to one single API call although it did lead me down the right path. 编辑:感谢您指出声明性字段CBroe(我更新了标题以更好地反映我想要的答案),但您标记的重复项未提及嵌套字段,这对于完全回答此问题并将其限制为单个API调用是必不可少的,尽管它确实做到了带我走正确的道路。 Complete solution for this which also returns the og data previously returned would be: 还可返回先前返回的og数据的完整解决方案是:

https://graph.facebook.com/v2.5/http%3A%2F%2Fwww.google.co.uk?access_token=xxxxx&fields=og_object{id,description,title,type,url,site_name,image,is_scraped,updated_time,audio,video,locale},id,share

Where fields= is our declarative fields and og_object is the top level JSON object - specifying this on its own however will only give us the trimmed down version of og_object minus the images. 其中fields=是我们的声明性字段,而og_object是顶级JSON对象- og_object指定此内容将只给我们修剪后的og_object版本减去图片。 In order to get the images we need to drill into og_object to get the images field using the nesting syntax for declarative fields: 为了获取图像,我们需要og_object以使用声明性字段的嵌套语法来获取images字段:

og_object{image}

and then we need to declare all the other og_object fields as they are no longer sent by default (plus a few others of interest), we would only get the image field 然后我们需要声明所有其他og_object字段,因为它们不再默认发送(加上一些其他感兴趣的字段),所以我们只会获取image字段

fields=og_object{id,description,title,type,url,site_name,image,is_scraped,updated_time,audio,video,locale},id,share

Single API call solution for this which also returns the og data previously returned would be: 为此,还返回先前返回的og数据的单个API调用解决方案是:

https://graph.facebook.com/v2.5/?id=http%3A%2F%2Fwww.google.co.uk?access_token=xxxxx&fields=og_object{id,description,title,type,url,site_name,image,is_scraped,updated_time,audio,video,locale},id,share

Where fields is our declarative fields and og_object is the top level JSON object - specifying this on its own however will only give us the trimmed down version of og_object minus the images. 其中, fields是我们的声明性字段,而og_object是顶级JSON对象-自行指定此内容,但是只会为我们提供og_object的精简版本减去图片。 In order to get the images we need to drill into og_object to get the images field using the nesting syntax for declarative fields: 为了获取图像,我们需要og_object以使用声明性字段的嵌套语法来获取images字段:

og_object{image}

and then we need to declare all the other og_object fields as they are no longer sent by default (plus a few others of interest), we would only get the image field 然后我们需要声明所有其他og_object字段,因为它们不再默认发送(加上一些其他感兴趣的字段),所以我们只会获取image字段

fields=og_object{id,description,title,type,url,site_name,image,is_scraped,updated_time,audio,video,locale},id,share

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM