facebook javascript - 发布为页面

时间:2016-08-16 20:15:17

标签: javascript facebook facebook-javascript-sdk

我正在尝试发布我管理页面的页面

以下代码中可能包含一些不必要的内容,但我试图涵盖所有基础

这是一个基本的html表,我们将从中提取数据并发布到fb

<table style="width: 100%;">
    <tr><td>1 col 1</td><td>1 col 2</td><td>1 col 3</td><td>1 col 4</td><td>1 col 5</td><td>1 col 6</td><td>1 col 7</td></tr>
    <tr><td>2 col 1</td><td>2 col 2</td><td>2 col 3</td><td>2 col 4</td><td>2 col 5</td><td>2 col 6</td><td>2 col 7</td></tr>
    <tr><td>3 col 1</td><td>3 col 2</td><td>3 col 3</td><td>3 col 4</td><td>3 col 5</td><td>3 col 6</td><td>3 col 7</td></tr>
</table>

脚本,我试图用每个部分评论它应该做什么

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : {APP_ID_HERE}, // unique app id
      version    : 'v2.7',
      cookie     : true,
      status     : true
    });
  };

// connect to facebook
(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'));

// set up what we are actually posting to facebook
(function($)
{
  $('tr').each(function()
  {
    var msg = $(this).find('td:nth-of-type(6)').text();
    $(this).find('td:first-of-type').append('<span class="left" onclick="fbPostIt(\'' + msg + '\')">[fb]</span>');
  })
})(jQuery);

// do the work
function fbPostIt(fbPost){
  var pageId = '{PAGE_ID_HERE}'; // facebook page id from page info

  // ensure we have permissions we need
  FB.login(function(){
    // used to get user accessToken
    var authResp = FB.getAuthResponse();

    // see what accounts user has access to
    FB.api('/me/accounts', 'get', {access_token : authResp.accessToken}, function(response){
console.log(response); // this is returning an object with the accounts
FB.api('/me/permissions', 'get', {access_token : pageAccessToken}, function(resp){console.log(resp)});
/**
 * permissions listed are "manage_pages", "publish_actions" and "public_profile"
 * all marked as "status : granted"
 */

      // find the page access token for the page we want to admin
      var pageAccessToken = ''; 
      for(i in response.data){
        if(response.data[i].id == pageId) {
          pageAccessToken = response.data[i].access_token;

          // do the actual post now
          FB.api('/' + pageId + '/feed', 'post', {
            message: fbPost,
            access_token : pageAccessToken
          }, function(info){
console.log(info);
/**
 * code    : 200
 * message : "(#200) The user hasn't authorized the application to perform this action"
 * type    : "OAuthException"
 */
          });

        }
      }
    });
  }, { scope: 'manage_pages, publish_actions' });
}
</script>

我不知道我在Facebook上的应用设置或代码中是否遗漏了某些内容,我完全没有看到代码有任何问题,但希望有些新鲜的眼睛可以帮助

当我第一次按下按钮发布时,Facebook会要求3组权限

  • 访问个人资料
  • 代表我发布
  • 管理网页

这三个我都说是

1 个答案:

答案 0 :(得分:2)

要张贴&#34;作为Page&#34;,您需要publish_pages,而不是publish_actions

API参考文档中记录了这一点:https://developers.facebook.com/docs/graph-api/reference/v2.7/page/feed#publish

顺便说一句,只有在用户未获得授权的情况下,并且在页面加载时使用FB.login,您才可以使用FB.getLoginStatus来改善此问题。例如:http://www.devils-heaven.com/facebook-javascript-sdk-login/