尽管使用范围变量,FB Javascript API仍未返回电子邮件

时间:2014-01-19 19:13:52

标签: javascript facebook facebook-javascript-sdk

我正在努力通过FB.login检索电子邮件。以下是我的代码:

$(document).ready(function() {
        $.ajaxSetup({ cache: true });
        $.getScript('//connect.facebook.net/en_US/all.js', function(){
          FB.init({
            appId: 'appId',
                    cookie: true,
                    oauth: true
          });     

          $('#fbRegister').on('click', function(){
              FB.login(function(response){
                  if(response.authResponse)
                  {
                      FB.api('/me?fields=email,name', function(responseFromFB){                                                           

                          var name = responseFromFB.name; 
                          var email = responseFromFB.email;

                          //Take the vales and send to 
                          $.ajax({

                                type: "POST",
                                url: 'php/register.php',        
                                async: false,
                                data: {'name':name,'email': email},
                                success: function(data)
                                {
                                   $('#fbRegister').hide();
                                   $('#successPrompt').show().text(data);

                                },
                                complete: function() {},
                                error: function(xhr, textStatus, errorThrown) 
                                {
                                  console.log('ajax loading error...');
                                  return false;
                                }
                         });

                      },{scope:'email,name'});
                  }
                  else
                  {
                      console.log('The login failed because they were already logged in');
                  }
              });                  
          });
        });
      }); 

在App Center权限中,我还为电子邮件配置了“用户和朋友”权限。似乎没有什么工作。我错过了什么吗?

3 个答案:

答案 0 :(得分:7)

您将范围参数作为参数传递给FB.api方法。它们需要提供给FB.login方法。试试这个:

$(document).ready(function() {
    $.ajaxSetup({ cache: true });
    $.getScript('//connect.facebook.net/en_US/all.js', function(){
      FB.init({
        appId: 'appId',
                cookie: true,
                oauth: true
      });     

      $('#fbRegister').on('click', function(){
          FB.login(function(response){
              if(response.authResponse)
              {
                  FB.api('/me?fields=email,name', function(responseFromFB){                                                           

                      var name = responseFromFB.name; 
                      var email = responseFromFB.email;

                      //Take the vales and send to 
                      $.ajax({

                            type: "POST",
                            url: 'php/register.php',        
                            async: false,
                            data: {'name':name,'email': email},
                            success: function(data)
                            {
                               $('#fbRegister').hide();
                               $('#successPrompt').show().text(data);

                            },
                            complete: function() {},
                            error: function(xhr, textStatus, errorThrown) 
                            {
                              console.log('ajax loading error...');
                              return false;
                            }
                     });

                  });
              }
              else
              {
                  console.log('The login failed because they were already logged in');
              }
          }, {scope:'email,public_profile'});                  
      });
    });
  }); 

答案 1 :(得分:0)

上面的答案几乎是完美的。唯一不起作用的是范围:'电子邮件,姓名' 应该是范围:'电子邮件,public_profile'

答案 2 :(得分:-1)

在按钮中传递@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); SharedPreferences appdata = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); etA.setText(appdata.getString("A", "")); etB.setText(appdata.getString("B", "")); } @Override public void onStop() { super.onStop(); SharedPreferences appdata = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); SharedPreferences.Editor editor = appdata.edit(); editor.putString("A", etA.getText().toString()); editor.putString("B", etB.getText().toString()); editor.commit(); }