使用 Facebook 登录后如何使用 JS SDK 获取用户信息 [英] How to get User info using JS SDK after they login with Facebook

查看:38
本文介绍了使用 Facebook 登录后如何使用 JS SDK 获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的标题中有这个:

<script src="http://connect.facebook.net/en_US/all.js"></script>

然后我有这样的FB登录按钮代码:

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&amp;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>

那么用户使用FB按钮登录后,我如何进行JS调用以获取他们的姓名,电子邮件,照片等?

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  : true, // parse XFBML
    channelURL : 'http://www.comehike.com/channel.html', // channel.html file
    oauth  : true // enable OAuth 2.0
  });
</script>

我需要上面的代码吗?

谢谢!!

推荐答案

<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>

获取登录的用户数据:

FB.api('/me', function(response) {
  console.log(response);
});

如果一切顺利response 应该是一个 JSON 对象,其中包含您有权查看的用户数据.

If all went well response should be a JSON object containing the user data you have permission to view.

用户图片可以从:

http://graph.facebook.com/FACEBOOK_USER_ID/picture

你可以用类型参数指定你想要的大小

You can specify the size you want with the type argument

  • 方形 - 50x50
  • 小 - 50 像素宽,高度可变
  • 正常 - 100 像素宽,高度可变
  • 大 - 大约 200 像素宽,高度可变

例如:

http://graph.facebook.com/FACEBOOK_USER_ID/picture?type=large

这篇关于使用 Facebook 登录后如何使用 JS SDK 获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆