使用Facebook JavaScript API发送电子邮件

4
我希望您能为我的应用程序翻译Facebook用户的电子邮件。但是,当我查询时,电子邮件地址返回为undefined。我不知道该如何处理它。
这是我的代码-
<body>
  <script>
    function statusChangeCallback(response) {
      console.log('statusChangeCallback');
      console.log(response);

      if (response.status === 'connected') {

        testAPI();
      } else if (response.status === 'not_authorized') {

        FB.login(function(response) {}, {
          scope: 'email'
        });
        document.getElementById('status').innerHTML = 'Please log ' +
          'into this app.';
      } else {
        FB.login(function(response) {}, {
          scope: 'email'
        });
        document.getElementById('status').innerHTML = 'Please log ' +
          'into Facebook.';
      }
    }

    function checkLoginState() {
      FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
      });
    }

    window.fbAsyncInit = function() {
      FB.init({
        appId: 'app id',
        cookie: true, // enable cookies to allow the server to access 

        xfbml: true, // parse social plugins on this page
        version: 'v2.1' // use version 2.1
      });

      FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
      });

    };

    (function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s);
      js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    function testAPI() {
      console.log('Welcome!  Fetching your information.... ');
      FB.api('/me', function(response) {
        alert(response.name);
        alert(response.email);
        console.log('Successful login for: ' + response.name);
        document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
      });
    }
  </script>
  <fb:login-button perms="email" autologoutlink="true" onlogin="checkLoginState();">
  </fb:login-button>
  <div id="status">
  </div>
</body>

你能否使用Graph API Explorer获取电子邮件? - Sahil Mittal
2个回答

8

@Tobi这真的救了我的一天:

FB.api('/me

被替换为

FB.api('/me?fields=id,name,email,permissions', function(response) {
        console.log(response.name);
        console.log(response.email);
    });

然而,我不明白为什么下面这段代码的范围(在Facebook文档中)没有起作用。
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">


1

alert(response.email) 给出的消息是未定义。 - swapnil gandhi
1
当您编辑代码时,是否再次看到登录弹出窗口要求 电子邮件 权限? - Sahil Mittal
4
FB.api('/me' 替换为 FB.api('/me?fields=id,name,email,permissions',并添加一个 alert(response.permissions)。然后检查是否存在 email 权限。 - Tobi
2
@swapnilgandhi,我们很难向您解释!我放弃了...好吧,最后一次尝试-无论何时您得到"[object Object]",您都可以使用console.log检查对象内部的内容,您怎么会不知道这个呢! - Sahil Mittal
@Tobi,已完成。谢谢。 - swapnil gandhi
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接