1. Apply for a Facebook account
2. log on to http://developers.facebook.com/apappid
3. Be sure to set Site URL and Site Domain for the APP you apply for. If you need to debug, you can set it to http: // localhost/and localhost
4. Reference JS on the page. <script src = "// connect.facebook.net/en_US/all.js"> </script>
5. Now you can call the API. Initialization is required before calling the API.
Call the following function for initialization:
[Javascript]
FB. init ({
AppId: '000000', // 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
});
Login:
[Javascript]
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 canceled login or did not fully authorize .');
}
}, {Scope: 'email, user_location, offline_access, publish_stream '});
}
Publish a message: To successfully publish a message, you must authorize the APP to send the message. Note that publish_stream in the above login code
[Javascript]
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; www.2cto.com
If (! Response | response. error ){
Alert ('error occured ');
} Else {
Alert ('Post ID: '+ response. id );
}
});
}
Author: diandian82