使用 Javascript 获取 Facebook 用户个人资料数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5846752/
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-08-23 19:10:56  来源:igfitidea点击:

Getting an Facebook user profile data with Javascript

javascriptfacebook

提问by lisovaccaro

I need to get a facebook user uid , gender, photo and other profile data with Javascript.

我需要使用 Javascript 获取 Facebook 用户 uid 、性别、照片和其他个人资料数据。

I remember there was a method to 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.

我记得有一种方法可以获取带有 .id、.gender、.photo 的用户对象,但我没有获得 API 调用的副本,并且在文档中找不到解释。

How do I get the user UID and gender with Javascript ?

如何使用 Javascript 获取用户 UID 和性别?

Thanks

谢谢

回答by user327961

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.

FB.getSession()2011 年对该问题的公认答案中使用的函数现在 (2012) 已弃用并从 Facebook JS SDK 中删除。

You can still discover the user's Facebook ID with the getLoginStatus()method like this:

您仍然可以使用以下getLoginStatus()方法发现用户的 Facebook ID :

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-rootelement in your HTML, like Aleadam explained in the accepted answer.

注意:您仍然需要初始化 Facebook API 并将fb-root元素放入 HTML 中,就像 Aleadam 在接受的答案中解释的那样。

回答by Aleadam

Update: I added a little more detail to the answer:

更新:我在答案中添加了更多细节:

First, you need to call FB.init and add your app id:

首先,您需要调用 FB.init 并添加您的应用程序 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>

您还需要添加<div id="fb-root"></div><body>页面的 ,并加载<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/

用户个人资料图片可以访问为 http:/graph.facebook.com/USERID/picture/

Check the Graph API reference here: http://developers.facebook.com/docs/reference/api/user/

在此处检查图形 API 参考:http: //developers.facebook.com/docs/reference/api/user/

and the FB SDK reference here: http://developers.facebook.com/docs/reference/javascript/

和这里的 FB SDK 参考:http: //developers.facebook.com/docs/reference/javascript/

回答by EazyHot

In the authorization parameterof your app, ensure that the Auth TokenParameter is configured as URI Fragment.

在您的应用程序的授权参数中,确保将Auth Token参数配置为URI Fragment

The login process using javascriptis explained in the facebookdocumentation there : Getting started with Facebook login.

使用javascript的登录过程在facebook文档中有说明:Facebook 登录入门

Follow the three steps, source code is already written.

按照三个步骤,源码已经写好了。