Facebook feed using graph api

Task: Get Facebook page feed using graph api.

Description: As facebook depreciated old feed url (https://www.facebook.com/feeds/page.php?format=rss20&id=page_id). So now we need to use its graph api for this.

Here I describe how to get facebook page feed using graph api using javascript code. You just need to update your access token ( use app token as access token from here https://developers.facebook.com/tools/accesstoken/ ). 

you can run below url on your browser for all fields and can use as your requirements. In below code limit is use for how many post will return.

https://graph.facebook.com/1399557183635849/feed?access_token=224210267619471|gtYtk8w5_qPHbZkSpJ9pwD7Qy-8

https://graph.facebook.com/page_id/feed?access_token=yourapptoken



<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="https://connect.facebook.net/en/all.js" type="text/javascript"></script>
<script>
 FB.api('https://graph.facebook.com/page_id/feed?access_token=yourapptoken', { limit: 20 }, function (response) {

 if (response && !response.error) {
 var msg='';
            for (var i = 0, l = response.data.length; i < l; i++) {
                var post = response.data[i];
                var userid = post.from.id;
                if (post.message) {                       
                    msg += '<span style="word-break: break-all;">'+ post.message  + '<br/><a  rel="nofollow" href='+post.link+'><img src=' +  post.picture  +' /></a>' + '</span><br/><br/>';                       
                }
                document.getElementById("sFbFeeds").innerHTML=msg;
            }
            }
            else
            {
               // alert('Error Occure');
            }
        });

        </script>
<div id="fb-root"></div>
<div id="sFbFeeds"></div>

No comments:

Post a Comment