2014-05-09 79 views
0

我想發佈到用戶爲admin的Facebook頁面。這裏是代碼我有使用JavaScript SDK發佈到Facebook頁面時的授權問題

FB.init({appId: "***********", status: true, cookie: true, frictionlessRequests: true}); 

var page = null; 

function postToPage(page, msg){ 
    FB.api('/'+page.id+'/feed', 'post', { message: msg, access_token: page.access_token }, 
    function(res) { console.log(res) } 
) 
} 



FB.login(function(response) { 
    if (response.authResponse) { 
    // user is logged in and granted some permissions. 
    FB.api('/me/accounts', function(response) { 
     console.log(response); 
     page = response.data[0]; 
     postToPage(page, 'hello fb-page world'); 
    }) 
    } else { 
     // User cancelled login or did not fully authorize. 
    } 
}, {scope:'read_stream,publish_stream,offline_access, manage_pages,status_update'}); 

它不工作,當我看到控制檯上,我有錯誤:

(#200) The user hasn't authorized the application to perform this action 

我缺少的scope的授權?

回答

2

嘗試了這一點

<body> 
<div id='fb-root'></div> 
<script src='http://connect.facebook.net/en_US/all.js'></script> 
<p> 
    <a href="javascript:;" onclick='postToFeed(); return false;'>Post to Group</a> 
</p> 
<p id='msg'></p> 

<script> 
    FB.init({appId: "Yourappid", status: true, cookie: true}); 

    function postToFeed() { 

    // calling the API ... 
     FB.api('/Pageid/feed', 'post', 
       { 
        message  : "It's awesome ...", 
        link  : 'Link', 
        picture  : 'Imageurl', 
        name  : 'Featured of the Day', 
        to: 'Pageid', 
        from: 'Pageid', 
        description : 'Your description' 
      }, 
      function(response) { 

       if (!response || response.error) { 
        alert(JSON.stringify(response.error)); 
       } else { 
        alert('Post ID: ' + response.id); 
       } 
      }); 
    } 

</script> 
</body> 

換種方式 主要挑戰是讓永久訪問令牌,這將讓我們代爲頁面管理的,其餘的是非常相似,使得一個普通的牆後。獲取訪問令牌需要向頁面管理員詢問manage_pages額外的權限,然後檢索其包含access_token字段的「帳戶」信息,以獲取具有管理權限的每個頁面。

FB.login(function() { 
    FB.api('/me/accounts', 'get', {}, function(response) { 
     console.log(response); 
    }); 
}, {perms:'publish_stream,offline_access,manage_pages'}); 

山口剛檢索其他的牆後的參數之間的訪問令牌

//API init code is omitted 
var wallPost = { 
    access_token: "<ACCESS_TOKEN>", 
    message: 'Hello, World!' 
}; 

FB.api('/me/feed', 'post', wallPost, function(response) { 
    if (!response || response.error) { 
     alert('Error occurred'); 
    } else { 
     alert('Success!'); 
    } 
}); 

關鍵是要獲得正確的訪問令牌。您可以嘗試在瀏覽器中使用螢火蟲並測試您在此獲得的訪問令牌https://developers.facebook.com/tools/debug/

相關問題