Javascript 在通过facebook登录的解析用户中保存其他字段
我正在制作一个应用程序,希望用户通过facebook登录。我的代码如下所示:Javascript 在通过facebook登录的解析用户中保存其他字段,javascript,facebook,parse-platform,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> <bod
<!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>
Myapp
函数fblogin(){
初始化(“APP_ID”,“JS_KEY”);
(功能(d、s、id){
var js,fjs=d.getElementsByTagName[0];
if(d.getElementById(id)){return;}
js=d.createElement;js.id=id;
js.src=“//connect.facebook.net/en_US/all.js”;
fjs.parentNode.insertBefore(js,fjs);
}(文档“脚本”、“facebook jssdk”);
window.fbAsyninit=函数(){
Parse.FacebookUtils.init({
appId:'xxxxxxxx',//Facebook应用程序ID
频道URL:'http://myapp.parseapp.com“,//通道文件
cookie:true,//启用cookie以允许解析访问会话
xfbml:true//解析xfbml
});
Parse.FacebookUtils.logIn('email,user\u friends'{
成功:功能(用户){
如果(!user.existed()){
警报(“用户注册并通过Facebook登录!”);
currentUser.set(“电子邮件”,email);
currentUser.save(空{
成功:函数(){alert('mail saved');},
错误:函数(){alert('mail saving failed');}
});
}否则{
警报(“用户通过Facebook登录!”);
}
},
错误:函数(用户,错误){
警报(“用户取消了Facebook登录或未完全授权”);
}
});
};
}
新的
但这段代码的作用是,它在我的解析应用程序的数据浏览器中创建了一个用户,但将“email”字段保留为“undefined”。我试图用上面的代码显式地保存电子邮件,但没有成功。我非常需要电子邮件与我的用户交流,它应该出现在“用户”类中。有没有办法做到这一点?您需要额外的权限才能访问电子邮件。在通话的get字符串中使用“scope=email”
请参阅:您需要额外调用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.");
}
});
请注意,只有在用户允许的情况下,您才能获取用户的电子邮件,这应该是因为您在登录权限中指定了它