簡體   English   中英

facebook javascript-作為頁面發布

[英]facebook javascript - post as a page

我正在嘗試發布為我管理的頁面的頁面

以下代碼可能包含一些不必要的內容,但我試圖涵蓋所有基礎知識

這是一個基本的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組權限

  • 訪問個人資料
  • 代我發帖
  • 管理頁面

我三個都同意

要以“頁面形式”發布,您需要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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM