如何使用图表api的facebook登录获取用户的电子邮件和fd id

时间:2013-03-07 10:34:12

标签: facebook facebook-graph-api facebook-javascript-sdk

我第一次使用facebook API工作。从那里我发现文件我们可以获取如下名称 的console.log(response.name); 但是,我怎样才能获取电子邮件和fbid?

谢谢, Enamul

<?php ?>
<div id="fb-root"></div>
<script>
  // Additional JS functions here
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'f', // App ID
      channelUrl : '//localhost/practices/phpTest/fblogin/login.php/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
    });
    FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        // connected
    } else if (response.status === 'not_authorized') {
        // not_authorized
      login();
    } else {
        // not_logged_in
      login();
    }
    });

    // Additional init code here

  };

  function login() {
    FB.login(function(response) {
        if (response.authResponse) {
            // connected
          testAPI();
        } else {
            // cancelled
        }
    });
    }

  function testAPI() {

    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {

        console.log('Good to see you, ' + response.name + '.');
    });
  }

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));
</script>
<?php ?>

4 个答案:

答案 0 :(得分:11)

除了添加范围=&#34;电子邮件&#34;在按钮上,您需要指定要返回的字段。

<fb:login-button scope="public_profile, email" onlogin="checkLoginState();" data-auto-logout-link="true" data-size="large">

function testAPI() {
    FB.api('/me', {fields: 'name, email'}, function(response) {
        console.log( response );
        console.log( response.email );
    });
}

有关完整的代码结构,请查看facebook documentation

答案 1 :(得分:3)

与访问名称的方式相同。即

response.emailresponse.id

但要获取电子邮件地址,您必须拥有访问该地址的权限。

scope="email"添加到您的FB登录按钮。

[编辑]

function login() {
    FB.login(function(response) {
        if (response.authResponse) {
            // connected
        testAPI();
        } else {
            // cancelled
        }
    }, { scope: 'email' });
    }

function testAPI() {

    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {

        console.log('Good to see you, ' + response.name + '.' + ' Email: ' + response.email + ' Facebook ID: ' + response.id);
    });
}

这是一个适合初学者的好教程:http://www.loginworks.com/technical-blogs/404-working-with-facebook-javascript-sdk

答案 2 :(得分:2)

如果其他人在检索用户的电子邮件时遇到麻烦,请注意,当您的应用仍列为“开发中”时,Facebook将不允许您访问用户的电子邮件。在Facebook Developer Dashboard中切换到“生产中”后,电子邮件应该可用。希望这对外面的人有帮助,因为我今晚浪费了大量时间努力检索用户的电子邮件。

电子邮件:“由于您的应用未启用,因此并非对所有用户都可用”

enter image description here

答案 3 :(得分:0)

将代码更改为以下内容 FB.api('/ me',function(response){console.log('见到您很高兴,'+ response.name +'。');});

FB.api('/ me?fields = name,email',function(response){console.log('见到您很高兴,'+ response.name +'---'+ response.email); });