0
我正在使用Facebook C#SDK,并获得了有关如何设置登录并获取一些用户信息以在我的网站上注册的示例。但是,我卡住了电子邮件信息。它始终为空。Facebook C#SDK,获取用户电子邮件的问题
我检查了一些在这里相关的帖子,但没有解决我的问题。我需要的所有信息都是正确的,只有电子邮件和生日不见了。
我在SHTML部分页面代码是这样的:
<div id="fb-root"></div>
<fb:login-button id="fblogin" onlogin="window.open('http://' + location.host + '/Facebook/LogOn', '_self')" perms='email'></fb:login-button>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '@Facebook.FacebookApplication.Current.AppId',
cookie: true,
xfbml: true,
oauth: true
});
function facebooklogin() {
FB.login(function (response) {
if (response.authResponse) {
// user authorized
//window.location.reload();
window.open("http://" + location.host + "/Facebook/LogOn", "_self")
} else {
// user cancelled
}
}, { scope: '@ViewBag.ExtendedPermissions' });
};
$(function() {
// make the button is only enabled after the facebook js sdk has been loaded.
$('#fblogin').attr('disabled', false).click(facebooklogin);
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
和我的Facebook控制器代码:
public ActionResult LogOn()
{
var fbWebContext = new FacebookWebContext(); // or FacebookWebContext.Current;
if (fbWebContext.IsAuthorized())
{
var fb = new FacebookWebClient();
dynamic me = fb.Get("me");
//Creating new User with Facebook credentials
var id = 0;
var nickname = me.first_name;
var password = "";
var email = me.email;
var picture = string.Format("http://graph.facebok.com/{0}/picture?type=large", me.id);
var thumbnail = string.Format("http://graph.facebok.com/{0}/picture?type=normal", me.id);
var fullName = me.name;
var gender = "";
if (me.gender == "male") gender = "Masculino";
if (me.gender == "female") gender = "Feminino";
var birthdate = Convert.ToDateTime(me.birthday);
//Does the user already exist?
if (DAUser.UserAlreadyExists(email))
{
ViewBag.Message = "O seu e-mail já foi cadastrado em nosso site. Por favor, cadastre outro e-mail.";
return View("~/Views/Shared/Erro.cshtml");
}
else
{
id = DAUser.Create(nickname, email, password, "facebook");
}
//Updating created user information
User user = db.User.Find(id);
TryUpdateModel(user);
user.Picture = picture;
user.Thumbnail = thumbnail;
user.Nickname = nickname;
user.FullName = fullName;
user.Gender = gender;
if (!String.IsNullOrEmpty(birthdate))
{
user.Birthdate = Convert.ToDateTime(birthdate);
}
db.SaveChanges();
Session["loginType"] = "Facebook";
return RedirectToAction("Mural", "Home");
}
else
{
ViewBag.Message = "Não foi possível completar o seu login via Facebook.";
return View("~/Views/Shared/Erro.cshtml");
}
}
如果我把这些线,作为示例所示:
[FacebookAuthorize(Permissions = ExtendedPermissions)]
if (fbWebContext.IsAuthorized(ExtendedPermissions.Split(',')))
我的应用程序停止工作。
任何人都有解决方案吗? O另一个示例代码获得授权请求电子邮件和生日?
编辑
问题是这一行:
}, { scope: '@ViewBag.ExtendedPermissions' });
在我的应用程序应该是:
}, { scope: 'email,user_birthday,user_hometown' }); //alter to get permission to access user data
我没有设定一个范围(烫发)Facebook的访问......但现在,我还有一个问题。
我的文化日期时间为DD/MM/YYYY和Facebook的生日设置为MM/DD/YYYY,我尝试这样做:
var birthdate = DateTime.Parse(me.birthday, CultureInfo.CreateSpecificCulture("pt-BR"));
的Cuz我在巴西的文化,但我仍然得到的DateTime错误:
String was not recognized as a valid DateTime.
任何人都知道如何解决这个问题?
我有一个类似的解决方案...不知道这是否会解决..但尝试offline_access,电子邮件,而不是电子邮件中的登录按钮。 – MoXplod