[英]How to get an image url from Facebook graph API for a given URL in a single call
我正在嘗試從Facebook圖形API中的共享鏈接獲取圖像URL數據。 我可以做到這一點,但是這樣做似乎很麻煩,因為它涉及到兩次服務器調用,並且依賴於舊的API版本,而我更希望使用當前版本的API(2.5版)來完成此操作。
直到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
它將返回與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"
}
由此,我可以使用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"
}
從那里我有一個圖片網址,我可以使用,但是在2.5中使用相同的方法來獲取圖片
{
"created_time": "2007-11-07T12:09:31+0000",
"title": "Google",
"type": "website",
"id": "383886459583"
}
有沒有一種方法可以使用ID或以更好的方式獲取2.5中的圖像數據,還是可以使用目標鏈接的URL進行一次調用並立即返回所有og數據的方法?
這方面的例子是
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
}
);
}
);
編輯:感謝您指出聲明性字段CBroe(我更新了標題以更好地反映我想要的答案),但您標記的重復項未提及嵌套字段,這對於完全回答此問題並將其限制為單個API調用是必不可少的,盡管它確實做到了帶我走正確的道路。 還可返回先前返回的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
其中fields=
是我們的聲明性字段,而og_object
是頂級JSON對象- og_object
指定此內容將只給我們修剪后的og_object版本減去圖片。 為了獲取圖像,我們需要og_object
以使用聲明性字段的嵌套語法來獲取images字段:
og_object{image}
然后我們需要聲明所有其他og_object字段,因為它們不再默認發送(加上一些其他感興趣的字段),所以我們只會獲取image字段
fields=og_object{id,description,title,type,url,site_name,image,is_scraped,updated_time,audio,video,locale},id,share
為此,還返回先前返回的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
其中, fields
是我們的聲明性字段,而og_object
是頂級JSON對象-自行指定此內容,但是只會為我們提供og_object
的精簡版本減去圖片。 為了獲取圖像,我們需要og_object
以使用聲明性字段的嵌套語法來獲取images字段:
og_object{image}
然后我們需要聲明所有其他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.