Call Facebook API on JS side

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.