简体   繁体   中英

Facebook Graph API get page post's attachments

I am trying to get the url of an image attachment published by a facebook page. The goal is to embed that image in a webpage in order for the website to always display the last image attachment published by the page. I own both website and FB page.

I have yet not grasped all the details on handling Facebook Graph API, but here is what I did so far:

1) in FB developers website, I have created an application, getting its App ID and secret;

2) I used that information to get an access token (just pasted in my browser the following code:

https://graph.facebook.com/oauth/access_token?client_id={my-client-id}&client_secret={my-client-secret}&grant_type=client_credentials

3) in my website, I have loaded Facebook JS SDK after the body tag; and got also my Facebook page ID from Facebook Page administration;

4) now the real question begins: how can I query Facebook to get the information that I need – the source url of the last published image?

The best result I have gotten so far was by making a getJSON call with the help of jQuery:

var fbfeed = $j.getJSON('https://graph.facebook.com/{my-page-id}/feed?access_token={my-token}&fields=attachments&limit=1');

This will get and store a JSON array in the fbfeed variable (please correct me if I'm wrong). One of the keys of that array is called "src" which contains the source url of the attachment – the information I need to embed that picture in my website;

I have the following problems / concerns: - I have not found the way to retrieve the value of the "url" key – how can I do that? How can I parse the fbfeed variable and extract the value of the "url" key?

– I have concerns with my usage of the access token:

  • is it problematic to expose the access token in this way, by using it in a jQuery function? Is it a security risk? If so, can I "mimic" this request but using a server side language such as PHP?

  • Will this access token expire (ie will I need to repeat step 2 from time to time?). So, imagining that I can get this to work, will I need from time to time to "refresh" the access token?

Thanks for your help.

I have managed to get the information I needed using server-side code, although it may not be the most "clean solution": it will iterate through the last 5 posts of my page until it finds an image and a post url:

<?php
$url = 'https://graph.facebook.com/{page-id}/feed?access_token={access-token}&fields=attachments,link&limit=5';
$json = file_get_contents($url);
$json_data = json_decode($json, true);

for($count = 0; $count < 5; $count++) {

$imagesource =  $json_data['data'][$count]['attachments']['data'][0]['media']['image']['src']; // gets the image url

$postlink = $json_data['data'][$count]['link']; // gets the post url

if (isset($imagesource) && isset($postlink)) {
    // do stuff with the image and post url
    break;
    };

};

// then I can do other stuff as fallback if the image url and post url are not found

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