在通过Facebook登录的解析用户中保存其他字段

时间:2014-08-15 03:26:33

标签: javascript facebook parse-platform

我正在创建一个应用程序,我希望用户通过Facebook登录。我的代码如下所示:

<!DOCTYPE html>
<html>
<head>

<title>Myapp</title>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.18.min.js"></script>

</head>

<body>

<script>
function fblogin(){
Parse.initialize("APP_ID", "JS_KEY");
(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/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));



      window.fbAsyncInit = function() {
  Parse.FacebookUtils.init({
    appId      : 'xxxxxxxx', // Facebook App ID
    channelUrl : 'http://myapp.parseapp.com', // Channel File
    cookie     : true, // enable cookies to allow Parse to access the session
    xfbml      : true  // parse XFBML
  });

  Parse.FacebookUtils.logIn('email,user_friends', {
  success: function(user) {
    if (!user.existed()) {
      alert("User signed up and logged in through Facebook!");
    currentUser.set("email",email);
    currentUser.save(null,{
    success: function(){  alert('mail saved');},
    error: function(){ alert('mail saving failed');}
    });
    } else {
      alert("User logged in through Facebook!");
    }
  },
  error: function(user, error) {
    alert("User cancelled the Facebook login or did not fully authorize.");
  }
});
};


}

    </script>


<h1>NEW</h1>

<input type="submit" value="Login with facebook" onclick="fblogin();"/>


</body>

</html>

但是这段代码的作用是它在我的Parse应用程序的数据浏览器中创建了一个用户,但是留下了&#34; email&#34; field as&#34; undefined&#34;。我试图在上面的代码中明确保存电子邮件,但没有成功。我非常需要电子邮件与我的用户进行沟通,它应该出现在班级&#34;用户&#34;中。有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:1)

您需要额外拨打Facebook API。这些方面的东西:

Parse.FacebookUtils.logIn('email,user_friends', {
    success: function(user) {
        if (!user.existed()) {
            alert("User signed up and logged in through Facebook!");

            FB.api('/me?scope=email', function(me) {
                user.set("email", me.email);
                user.save();
            });
        } else {
            alert("User logged in through Facebook!");
        }
    },
    error: function(user, error) {
        alert("User cancelled the Facebook login or did not fully authorize.");
    }
});

请注意,如果用户允许,您只能获取用户的电子邮件,这应该是您在登录权限中指定的情况。

答案 1 :(得分:0)

您需要额外的权限才能访问电子邮件。在通话的获取字符串中使用“scope = email”。

请参阅:https://stackoverflow.com/a/3611781/1397632