Get Facebook user information through the JavaScript Facebook API, as well as the current user's good

Source: Internet
Author: User

You need to be a Facebook app ID before you use the app, which doesn't have to be said.

Initialize Facebook information in JavaScript after generating the Facebook APP ID

And I've defined some of the global variables I need to get the data.

<script language= "javascript" type= "Text/javascript" >

This is the information variable we're going to get.

var Facebookid;

var facebookname;

var facebookprofileimg;

var facebookbooks;

var facebookmovies;

var facebookmusic;

var facebooklocations;

var friendfbids = ';

var Facebookemail;

Fb.init ({

AppId: ' 138343092941586 ',//Here Set the app ID you're applying for

Status:true,

Cookie:true,

Xfbml:true,

Oauth:true

});

</script>

Copy Code

Once the initialization is complete, you can trigger the ability to get Facebook information through an event. (I used the ID of the click event)

<script language= "javascript" type= "Text/javascript" >

$ (' #invite '). Click (function () {

Fb.getloginstatus (Checkresponse);

});

The Checkresponse method is used to detect whether the current user is logged in.

function Checkresponse (response) {

if (! Response.authresponse) {

Scope is the means to get permission, you can get other such as: Offline_access, Publish_stream and other permissions

Fb.login (handlesessionresponse,{scope: ' email '});

} else {

This method is the way to get Facebook user information

Retrieveprofiles ();

}

}

The second method is to handle the Facebook login response accordingly.

function Handlesessionresponse (response) {

if (! Response.authresponse) {

Return

} else {

This method is the way to get Facebook user information

Retrieveprofiles ();

}

}

</script>

Copy Code

Here's the point here, based on the Facebook information you get above, you can get information about your Facebook database via FACEBOOKAPI.

<script language= "javascript" type= "Text/javascript" >

Used to cycle through the Facebook friend ID

Object.size = function (obj) {

var size = 0;

for (var key in obj) {

if (Obj.hasownproperty (key)) size++;

}

return size;

}

function Retrieveprofiles () {

Query user information via Facebook API

Fb.api ({method: ' fql.query ', query: ' SELECT uid,email, name, Pic_square, books, movies, music, current_location from user WHERE uid=me () '}, function (response) {

Fetch the status from response

var user = response[0];

if (user = = undefined) {

$ (' span.loading '). ReplaceWith (' <span>sorry, please try Again.</span> ');

} else {

Facebookid = User.uid;

Facebookname = User.Name;

facebookprofileimg = User.pic_square;

Facebookmovies = user.movies;

Facebookmusic = User.music;

Facebookbooks = User.books;

Facebookemail = User.email;

Facebooklocations = user.current_location;

Get the current user's Facebook friends

Fb.api ({method: ' fql.query ', query: ' SELECT uid2 from friend WHERE uid1 = ' + Facebookid}, function (data) {

Console.log (data);

Note that the Object.size method is used here.

for (var i = 0; I < object.size (data); i++) {

Friendfbids = friendfbids + "'" + Data[i].uid2 + "'";

if (i! = object.size (data)-1) {

Friendfbids + = ', '; treatment of Yan Chai psychiatric disease in Wuhan

}

}

Console.log (Friendfbids);

Console.log ("Facebookname" +facebookname);

Console.log ("Facebookemail" +facebookemail);

Console.log ("facebookprofileimg" +user.pic_square);

Console.log ("Facebookmovies" +user.movies);

Console.log ("Facebookmusic" +user.music);

Console.log ("Facebookbooks" +user.books);

Console.log ("Facebooklocations" +user.current_location);

});

}

});

}

</script>

Copy Code http://www.dztxn.com/space/?342/log/829

Get Facebook user information through the JavaScript Facebook API, as well as the current user's good

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.