1.首先要先到Facebook Developer中建立一個 Application,取得App ID:
2.設定App的基本資料
3.由Auth Dialog 設定App要取得使用者的權限
4. 使用FB的Javascript SDK 在自己的網站上撰寫code
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: '1970*******7670', status: true, cookie: true,
xfbml: true});
};
function myfbShare(){
var params = {};
params['message'] = 'message';
params['name'] = 'name';
params['description'] = 'description';
params['link'] = 'http://www.yahoo.com';
params['picture'] = 'http://www.yahoo.com/logo.jpg';
params['caption'] = 'caption';
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
console.log(response.error);
} else {
console.log("Publish");
}
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'publish_actions,user_likes'});
}
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/zh_TW/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}()); // 把聯外通需要的 js 檔放到 fb-root 這個 div 裡
</script>
其中scope的設定要有publish_actions這樣才能取得使用者的權限,相關的scope設定可以查這邊
留言