如何通过Facebook登录按钮获取用户电子邮件?

时间:2015-09-18 02:16:24

标签: javascript facebook facebook-javascript-sdk

我想创建一个Facebook登录按钮。其实我做了,但我需要获取用户名,电子邮件和其他信息。我怎么能这样做?

这是我的代码

<html>
<head></head>
<body>
<div id="fb-root"></div>

<script>
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/tr_TR/sdk.js#xfbml=1&version=v2.4&appId=168750412481****";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

</script>

<div class="fb-login-button" data-max-rows="1" data-size="large" data-show-faces="false" data-auto-logout-link="true" data-scope="email"></div>
</body>
</html>

如何获取数据范围用户电子邮件或其他范围属性?

2 个答案:

答案 0 :(得分:4)

Accualy我需要完整的代码示例。因为我认为facebook文件很复杂。 (或者对我来说)我找到了解决方案互联网。在这里你可以看到;

FB登录按钮:

<div class="fb-login-button" data-max-rows="1" data-size="large" data-show-faces="false" data-auto-logout-link="true" data-scope="email,user_hometown,user_birthday,user_education_history,user_website,user_work_history"></div>

FB Sdk初始化:

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/tr_TR/sdk.js#xfbml=1&version=v2.4&appId=***********";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

获取其他用户信息:

  window.fbAsyncInit = function() {
    FB.init({
      appId      : '************', // Set YOUR APP ID
      channelUrl : 'http://hayageek.com/examples/oauth/facebook/oauth-javascript/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true,  // parse XFBML
      version    : 'v2.4',
      oauth      : true
    });

    FB.Event.subscribe('auth.authResponseChange', function(response) 
    {
     if (response.status === 'connected') 
    {
        console.log("<br>Connected to Facebook");
        //SUCCESS
        FB.api('/me', { locale: 'tr_TR', fields: 'name, email,birthday, hometown,education,gender,website,work' },
          function(response) {
            console.log(response.email);
            console.log(response.name);
            console.log(response.gender);
            console.log(response.birthday);
            console.log(response.hometown);
            console.log(response.education);
            console.log(response.website);
            console.log(response.work);
          }
        );

    }    
    else if (response.status === 'not_authorized') 
    {
        console.log("Failed to Connect");

        //FAILED
    } else 
    {
        console.log("Logged Out");

        //UNKNOWN ERROR
    }
    }); 

    };

答案 1 :(得分:0)

查看

上的文档

了解如何进行登录和查询图谱API ...