facebook javascript - post 作为一个页面

facebook javascript - post as a page

我正在尝试 post 作为我管理的页面的页面

下面的代码可能有一些不必要的部分,但我试图涵盖我的所有基础

这是一个基本的htmltable,我们将从中提取数据并post到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 上的应用程序设置中是否遗漏了某些内容,或者在代码中,我没有发现代码有任何问题,但希望一些新鲜的眼光可以提供帮助

当我第一次按下 post 按钮时,facebook 要求 3 组权限

三个我都同意

要发布 "as Page",您需要 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/