Getting an Facebook user profile data with Javascript
I need to get a facebook user uid , gender, photo and other profile data with Javascript.
I remember there was a method t开发者_JAVA百科o get an user object with .id, .gender, .photo but I haven't got a copy of the API call and I can't find an explanation in the documentation.
How do I get the user UID and gender with Javascript ?
Thanks
The function FB.getSession()
used in the accepted answer to this question in 2011 is now (2012) deprecated and removed from the Facebook JS SDK.
You can still discover the user's Facebook ID with the getLoginStatus()
method like this:
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert ("Your UID is " + response.authResponse.userID);
}
});
Note: you still have to init the Facebook API and put the fb-root
element in your HTML, like Aleadam explained in the accepted answer.
Update: I added a little more detail to the answer:
First, you need to call FB.init and add your app id:
FB.init(
{
appId : APP_ID,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
Next, check if there is a session open (i.e., the user is logged in)
if(FB.getSession() != null) {
And query the details by:
FB.api('/me', function(response)
{
alert ("Welcome " + response.name + ": Your UID is " + response.id);
});
}
You will also need to add <div id="fb-root"></div>
to the <body>
of your page, and load <script src="http://connect.facebook.net/en_US/all.js"></script>
The user profile picure can be accessed as http:/graph.facebook.com/USERID/picture/
Check the Graph API reference here: http://developers.facebook.com/docs/reference/api/user/
and the FB SDK reference here: http://developers.facebook.com/docs/reference/javascript/
In the authorization parameter of your app, ensure that the Auth Token Parameter is configured as URI Fragment.
The login process using javascript is explained in the facebook documentation there : Getting started with Facebook login.
Follow the three steps, source code is already written.
精彩评论