最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get most recent post from Facebook page, and display on website using new Graph API JavaScript only - Stack Overflow

programmeradmin0浏览0评论

I'm building a website for a business. The business currently has a Facebook page. On the website i'm creating, i need to display the most recent Facebook post.

A Google search led me to a depreciated method, and Facebook says now to use the new Graph API, but i'm starting from no Facebook API experience so found it overwhelming and could use some help.

Here's where i am.

I'm doing this as a single page site with no server side language, so am only using JavaScript. So far I've got the FB JS SDK script after my body tag, and a app-id setup for my page.

<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'your-app-id', // except real id
            xfbml: true,
            version: 'v2.4'
        });
    };

    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) { return; }
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

I have linked my FB app with my FB page. Now i need to display the most recent post on my website.

I'm building a website for a business. The business currently has a Facebook page. On the website i'm creating, i need to display the most recent Facebook post.

A Google search led me to a depreciated method, and Facebook says now to use the new Graph API, but i'm starting from no Facebook API experience so found it overwhelming and could use some help.

Here's where i am.

I'm doing this as a single page site with no server side language, so am only using JavaScript. So far I've got the FB JS SDK script after my body tag, and a app-id setup for my page.

<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'your-app-id', // except real id
            xfbml: true,
            version: 'v2.4'
        });
    };

    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) { return; }
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

I have linked my FB app with my FB page. Now i need to display the most recent post on my website.

Share Improve this question edited Mar 8, 2021 at 16:32 SomeRandomDeveloper asked Aug 28, 2015 at 14:51 SomeRandomDeveloperSomeRandomDeveloper 4892 gold badges13 silver badges34 bronze badges 2
  • 3 With API v2, you need an access token to request this kind of data. So if you want to do that client-side only, then you have to make users login with their FB account first. Server-side you could use your app access token or a page access token – but those must never be exposed in client-side code. Other than that, your only option would be to use the Page Plugin – that has an option to display the last X posts of the page. – C3roe Commented Aug 31, 2015 at 10:37
  • 1 Thanks, i guess the page plugin is the only way then, so this is technically the answer if you want to answer this. – SomeRandomDeveloper Commented Sep 10, 2015 at 18:56
Add a ment  | 

2 Answers 2

Reset to default 1
FB.api(
    "/{page-id}/feed",
    function (response) {
      if (response && !response.error) {
        /* handle the result */
      }
    }
);

Source: https://developers.facebook./docs/graph-api/reference/v2.4/page/feed

You could just use the following API call to get only one entry:

/{page-id}/feed?limit=1

    window.fbAsyncInit = function() {
        FB.init({
          appId            : 'app id here',
          autoLogAppEvents : true,
          xfbml            : true,
          version          : 'v3.2'
        });
    
    //For getting posts from wall,
    	FB.api(
    		'/me',
    		{access_token:'Access token here',
    		fields:"id,name,posts{attachments}"},
    		function(response) {
    			console.log(response)
    		}
    	);
//for getting posts from page we manage
    	FB.api(
        "/page_id/feed",
    	{access_token:'access token',
    	fields:"attachments{url,media,description,title,type},created_time,description,message,updated_time"},
        function (response) {
    	console.log(response)
    	  if (response && !response.error) {
            /* handle the result */
          }
        }
    );
    };    
    (function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "https://connect.facebook/en_US/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论