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

use*_*414 32 email facebook facebook-javascript-sdk facebook-apps

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

mas*_*tic 97

// 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);
  }
);
Run Code Online (Sandbox Code Playgroud)


Man*_*nas 14

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

<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>
Run Code Online (Sandbox Code Playgroud)


Tom*_*yBs 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完成.


Ram*_*wad 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>
Run Code Online (Sandbox Code Playgroud)

使用它来获得额外的许可

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