简体   繁体   中英

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. 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.

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

and it would return the same as the 2.5 version of the API:

{
   "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:

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

{
   "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?

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. Complete solution for this which also returns the og data previously returned would be:

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. 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{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

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:

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. 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{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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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