使用facebook API javascript获取电子邮件

Ste*_*ero 2 javascript facebook

我有这个方法,我想收到一个人的电子邮件和发送电子邮件=未定义.

第一次尝试:

     function testAPI() {
    FB.login(function(response) {
     if (response.authResponse) {
       console.log('Welcome!  Fetching your information.... ');
       FB.api('/me', function(response) {
         console.log('Good to see you, ' + response.email + '.');
         alert('Good to see you, ' + response.email + '.');
       });
     } else {
       console.log('User cancelled login or did not fully authorize.');
     }
   }, {scope:'email'});
Run Code Online (Sandbox Code Playgroud)

我正在搜索,我发现我必须使用{scope:image},如果我把它,不要告诉我警报

And*_*rea 24

取而代之

FB.api('/me', function(response) {
  // ...
Run Code Online (Sandbox Code Playgroud)

有了这个

FB.api('/me?fields=id,name,email,permissions', function(response) {
  console.log(response.name);
  console.log(response.email);
  // ...
Run Code Online (Sandbox Code Playgroud)

我可以收到用户的电子邮件.


Gio*_*rio 6

你的语法错了,这是正确的:

function testAPI() {
    FB.login(
        function(response) {
            if (response.authResponse) {
               console.log('Welcome!  Fetching your information.... ');
               FB.api('/me', function(response) {
                   console.log('Good to see you, ' + response.email + '.');
                   alert('Good to see you, ' + response.email + '.');
               });
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }
        },
        {scope:'email'}
        );
}
Run Code Online (Sandbox Code Playgroud)

其中的{scope:'email'}内容)作为FB.login的第二个参数


njm*_*was 6

或者使用

FB.init('/me', {"fields":"id,name,email,first_name,last_name"}, function(response){
console.log(response.email);
});
Run Code Online (Sandbox Code Playgroud)