1. 申請Facebook賬號
2.登陸http://developers.facebook.com/申請APPID
3.一定要對你申請的APP設置Site URL 和Site Domain, 如果你需要調試,可以設置爲http://localhost/和localhost
4.在頁面中引用JS, <script src="//connect.facebook.net/en_US/all.js"></script>
5.現在就可以調用API了,在調用API之前,必須要進行初始化
調用如下函數進行初始化:
FB.init({
appId: '447540288619439', // App ID
channelUrl: 'http://example.com/channel.html', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
oauth : true
});
登陸:
function Test() {
// FB.api('/me', function (response) {
// alert('Your name is ' + response.name);
// });
FB.login(function (response) {
if (response.authResponse) {
alert('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
alert('Good to see you, ' + response.name + '.');
});
} else {
alert('User cancelled login or did not fully authorize.');
}
}, { scope: 'email,user_location,offline_access,publish_stream' });
}
發佈消息:要成功發佈消息,必須讓APP取得發送權限,注意上面登陸代碼中的publish_stream
function PostTest() {
var picurl = "http://102.mlsimages.movoto.com/064/12048664_0.jpg";
var body = 'Hello every one, this is my house!';
FB.api('/me/feed', 'post', { message: body, picture:picurl }, function (response) {
debugger;
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
其他參數請見:http://developers.facebook.com/docs/reference/api/post/