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/