How to get User info using JS SDK after they login with Facebook
I have this in my header:
<script src="http://connect.facebook.net/en_US/all.js"></script>
Then I have the FB login button code like this:
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=myAppId&xfbml=1">
</script><fb:login-button show-faces="false" perms="user_hometown,user_about_me,email,user_address"
autologoutlink="true" width="200" max-rows="1">
</fb:login-button>
Then after the user logs in with the FB button, how do I make a JS call to get their name, email, photo, etc?
I also found some code like this, but not sure where this is used:
<script>
FB.init({
appId : 'myAppId',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml开发者_如何学Go : true, // parse XFBML
channelURL : 'http://www.comehike.com/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
</script>
Do I need the code right above?
Thanks!!
<html>
<head> ... </head>
<body>
<div id="fb-root"></div>
<fb:login-button show-faces="false" perms="user_hometown,user_about_me,email,user_address" autologoutlink="true" width="200" max-rows="1"></fb:login-button>
<!-- put this before the end body tag -->
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'myAppId',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
channelURL : 'http://www.comehike.com/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
</script>
</body>
</html>
To get logged users data:
FB.api('/me', function(response) {
console.log(response);
});
If all went well response
should be a JSON object containing the user data you have permission to view.
The users image can be gotten from:
http://graph.facebook.com/FACEBOOK_USER_ID/picture
You can specify the size you want with the type argument
- square - 50x50
- small - 50 pixels wide, variable height
- normal - 100 pixels wide, variable height
- large - about 200 pixels wide, variable height
For example:
http://graph.facebook.com/FACEBOOK_USER_ID/picture?type=large
精彩评论