从Facebook页面获取最新帖子,并仅使用新的Graph API JavaScript在网站上显示

时间:2015-08-28 14:51:11

标签: javascript facebook facebook-graph-api

我正在为企业建立一个网站。该公司目前有一个Facebook页面。在我创建的网站上,我需要显示最新的Facebook帖子。

谷歌搜索引导我采用折旧方法,Facebook现在表示要使用新的图谱API,但我从没有Facebook API经验开始,所以发现它压倒一切并且可以使用一些帮助。

我在哪里。

我没有作为没有服务器端语言的单页网站而这样做,所以我只使用JavaScript。到目前为止,我已经在我的body标签之后获得了FB JS SDK脚本,并为我的页面设置了app-id设置。

<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.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

我已将我的FB应用与我的FB页面相关联。现在我需要在我的网站上显示最新的帖子。

2 个答案:

答案 0 :(得分:1)

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

来源:https://developers.facebook.com/docs/graph-api/reference/v2.4/page/feed

您只需使用以下API调用即可获得一个条目:

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

答案 1 :(得分:-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.net/en_US/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));

相关问题