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

发布于 2024-12-11 07:20:32 字数 976 浏览 0 评论 0原文

我正在使用 JavaScript API 为 Facebook 创建应用程序。问题是,它正在返回
电子邮件=未定义

我不知道为什么?如果我在应用程序上使用 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>

I am using JavaScript API to create my app for Facebook. The problem is, it's returning
email = undefined.

I don't know why? And if I use Facebook login/logout button on my app then the alert shows correct email id of the user but I don't want to do that.
What am I missing?

Here is my code:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

虚拟世界 2024-12-18 07:20:33

根据 Facebook 页面上的最新信息,您应该使用“范围”而不是权限。
https://developers.facebook.com/docs/reference/javascript/FB.login/

如果您访问

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

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

According to the latest info on the facebook page you should use 'scope' instead of perms.
https://developers.facebook.com/docs/reference/javascript/FB.login/

If you visit

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

and use the fb-api -> user-info example as a starting point, then logout and back in again, it should ask you for email perms and you can see your email being printed. It is done using response.email as you mention in your post.

暖心男生 2024-12-18 07:20:33
<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/

<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>

Use this to for extra permission

for more details visit :
https://www.fbrell.com/examples/

要走就滚别墨迹 2024-12-18 07:20:33

在此代码中,我从 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'});

In this code i have get user data form facebook and store into my database using 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'});
天煞孤星 2024-12-18 07:20:33

您的解决方案有一些问题。首先,您使用的是旧的身份验证方案。您应该使用此处描述的新版本:
https://developers.facebook.com/docs/reference/javascript/

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

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

您可以通过使用 FB.login 函数来获取这些信息,如 TommyBs 在另一个答案中所述。

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

There are a couple of things wrong with your solution. First of all you are using the old authentication scheme. You should use the new one described here :
https://developers.facebook.com/docs/reference/javascript/

You need to add the oauth:true to your init function, and make sure that your getLoginStatus looks for the new type of response.

When that is said you need to make sure you have the right permissions to see the users e-mail. You can see the required permissions here:
http://developers.facebook.com/docs/reference/api/user/

You get those by using the FB.login function as described by TommyBs in another answer.

Once you have those options you can use the FB.api function to get the e-mail.

指尖微凉心微凉 2024-12-18 07:20:32
// 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);
  }
);
// 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);
  }
);
失眠症患者 2024-12-18 07:20:32

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

<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>

here is example how i retrieve user name and e-mail:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文