如何使用javascript sdk获取Facebook用户的电子邮件ID

问题描述 投票:31回答:6

我正在使用JavaScript API为Facebook创建我的应用程序。问题是,它正在回归 email = undefined

我不知道为什么?如果我在我的应用程序上使用Facebook登录/注销按钮,则警报显示用户的正确电子邮件ID,但我不想这样做。我错过了什么?

这是我的代码:

<p><fb:login-button autologoutlink="true" perms="user_about_me,email"></fb:login-button></p>

<script>
window.fbAsyncInit = function () {
  FB.init({ appId: '250180631699888', status: true, cookie: true,
  xfbml: true
});

  FB.getLoginStatus(function (response) {
    if (response.session) {
      greet();
    }
  });
};
(function () {
  var e = document.createElement('script');
  e.type = 'text/javascript';
  e.src = document.location.protocol +
  '//connect.facebook.net/en_US/all.js';
  e.async = true;
  document.getElementById('fb-root').appendChild(e);
} ());

function greet() {
  FB.api('/me', function (response) {
  alert('Welcome, ' + response.name + "!");
  alert('Your email id is : '+ response.email);
});
}
</script>
facebook email facebook-javascript-sdk facebook-apps
6个回答
9
投票

根据facebook页面上的最新信息,你应该使用'scope'而不是perms。 https://developers.facebook.com/docs/reference/javascript/FB.login/

如果你访问

https://developers.facebook.com/tools/console/

并使用fb-api - > user-info示例作为起点,然后注销并重新登录,它应该询问您的电子邮件权限,并且您可以看到您的电子邮件正在打印。如您在帖子中提到的那样,它使用response.email完成。


94
投票
// https://developers.facebook.com/docs/javascript/reference/FB.api/
// v2.4

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

13
投票

这是我如何检索用户名和电子邮件的示例:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>

<script>
  $(function() {
    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
    });

    FB.getLoginStatus(function(response) {
      if (response.status == 'connected') {
        getCurrentUserInfo(response)
      } else {
        FB.login(function(response) {
          if (response.authResponse){
            getCurrentUserInfo(response)
          } else {
            console.log('Auth cancelled.')
          }
        }, { scope: 'email' });
      }
    });

    function getCurrentUserInfo() {
      FB.api('/me', function(userInfo) {
        console.log(userInfo.name + ': ' + userInfo.email);
      });
    }
  });
</script>

8
投票
<button id="fb-login">Login & Permissions</button>

<script>
document.getElementById('fb-login').onclick = function() {
  var cb = function(response) {
    Log.info('FB.login callback', response);
    if (response.status === 'connected') {
      Log.info('User logged in');
    } else {
      Log.info('User is logged out');
    }
  };
  FB.login(cb, { scope: 'email' });
};
</script>

使用它来获得额外的许可

欲了解更多详情,请访问:https://www.fbrell.com/examples/


1
投票

在这段代码中,我从facebook获取用户数据并使用ajax存储到我的数据库中

 FB.login(function(response) {
          if (response.authResponse) {
         FB.api('/me?fields=email,name,first_name,last_name', function(response) 
         {
             FB.api(
             "/"+response.id+"/picture?height=100",
                                    function (responses) {
                                        //console.log(responses.data.url)
                                                    response['profile_pic']=responses.data.url;
                                        $.ajax({
                                                   type:"POST",
                                                   url:'<?php echo base_url(); ?>'+'home/facebook_get_signup',
                                                   data:response,
                                                   success:function(res)
                                                   {
                                                       if(res=='success')
                                                       {

                                                           window.location='<?php echo base_url(); ?>';
                                                       }
                                                       if(res=='exists')
                                                       {
                                                           window.location='<?php echo base_url(); ?>';  
                                                       }
                                                   }
                                               });

                                    }
                                )
         });
        } else {
         console.log('User cancelled login or did not fully authorize.');
        } 
      // handle the response
        }, {scope: 'email,user_likes'});

0
投票

您的解决方案存在一些问题。首先,您使用旧的身份验证方案。你应该使用这里描述的新的:https://developers.facebook.com/docs/reference/javascript/

您需要将oauth:true添加到init函数中,并确保getLoginStatus查找新类型的响应。

说完后,您需要确保您具有查看用户电子邮件的权限。您可以在此处查看所需的权限:http://developers.facebook.com/docs/reference/api/user/

你可以通过使用TommyBs在另一个答案中描述的FB.login函数来获得这些。

有了这些选项后,您可以使用FB.api函数来获取电子邮件。

© www.soinside.com 2019 - 2024. All rights reserved.