【问题标题】:Facebook oauth retrieve user EmailFacebook oauth 检索用户电子邮件
【发布时间】:2011-04-18 18:27:45
【问题描述】:

我正在使用 Facebook Javascript SDK 获取 oauth 令牌并从 FB 检索所有数据:

 function login() {
    FB.api('/me', function (response) {            
        alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");

    });

我检索到所有基本信息(全名、工作、学校、ID..),但不是 EMAIL(response.email == undefined)!!

我知道可以在基本信息中访问电子邮件(不要求“电子邮件”权限),我该怎么做?

【问题讨论】:

    标签: facebook facebook-graph-api


    【解决方案1】:

    您需要按如下方式明确获取电子邮件:

                        var url = '/me?fields=name,email';
                        FB.api(url, function (response) {
                            alert(response.name);
                            alert(response.email);
                        });
    

    要获取电子邮件地址,需要电子邮件权限。所以代码可能看起来像(还有其他选项,如注册按钮、登录按钮..)

                FB.login(function (response) {
                    if (response.session) {
                        var url = '/me?fields=name,email';
                        FB.api(url, function (response) {
                            alert(response.name);
                            alert(response.email);
                        });
                    }
                    else {
                        alert("User did not login successfully");
                    }
                }, { scope: 'email' }); /* perms changed to scope */
    

    【讨论】:

      【解决方案2】:

      首先让您的应用上线,向公众开放。然后,您必须授予权限以获取应用程序默认不允许的用户“电子邮件”。之后,登录方法应更改如下。

      function checkLoginState() {
          FB.getLoginStatus(function(response) {
              statusChangeCallback(response);
              console.log('Welcome!  Fetching your information.... ');
              var url = '/me?fields=id,name,email';
              FB.api(url, function(response) {
                   alert(response.name + " " + response.id + " " +response.email);
              }, {scope: 'email'});
          });
      }
      

      如果不需要字段 id 将变量 url 更改为:

      var url = '/me?fields=name,email';
      

      【讨论】:

      • 在哪里授予权限以获取用户电子邮件?
      • 转到 facebook 开发者控制台。
      • 在 App Review 下的控制台中,显示电子邮件默认已批准。但是,我仍然没有得到一个 Facebook 应用程序的电子邮件地址,而另一个是好的。我找不到任何电子邮件的特殊设置。
      【解决方案3】:

      如果您可以更改为使用承诺链,这对我有用:

      fb.login({scope:'email'}).then(function (resp) {
          if (resp.status === 'connected') {
             fb.api('/me', 'get', {fields:'id,email,first_name,gender,...'}).then(function (response) {
                alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");
             });
          }
      });
      

      【讨论】:

        【解决方案4】:

        这是不可能的,用户必须允许您的应用程序访问他的电子邮件信息,或者在其他情况下,如果用户在他的隐私设置中允许所有人查看他的电子邮件,则它可以访问。

        【讨论】:

        • 我也有同样的情况。当需要电子邮件时,我无法使用我的帐户登录 fb oauth,因为我不允许我的电子邮件公开可见
        猜你喜欢
        • 1970-01-01
        • 2017-09-04
        • 2015-04-08
        • 1970-01-01
        • 2021-03-23
        • 2016-03-27
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多