javascript 使用 Facebook 登录后如何使用 JS SDK 获取用户信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6915860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:19:32  来源:igfitidea点击:

How to get User info using JS SDK after they login with Facebook

javascriptfacebook-graph-api

提问by Genadinik

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:

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

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

Then after the user logs in with the FB button, how do I make a JS call to get their name, email, photo, etc?

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

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>

Do I need the code right above?

我需要上面的代码吗?

Thanks!!

谢谢!!

回答by jBit

<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 responseshould be a JSON object containing the user data you have permission to view.

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

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

您可以使用 type 参数指定所需的大小

  • square - 50x50
  • small - 50 pixels wide, variable height
  • normal - 100 pixels wide, variable height
  • large - about 200 pixels wide, variable height
  • 方形 - 50x50
  • 小 - 50 像素宽,高度可变
  • 正常 - 100 像素宽,可变高度
  • 大 - 大约 200 像素宽,高度可变

For example:

例如:

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