获取用户电子邮件的问题

获取用户电子邮件的问题

本文介绍了Facebook C# SDK,获取用户电子邮件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Facebook C# SDK,并获得了一个关于如何设置登录和获取一些用户信息以注册我的网站的示例.但是,我停留在电子邮件信息中.它始终为空.

I'm using Facebook C# SDK and got a sample on how to set up a login and get some user information to register im my website. However, I stuck in email information. It's always null.

我查看了此处相关的一些帖子,但没有一个能解决我的问题.我需要的所有信息都正确提供,只是缺少电子邮件和生日.

I've checked some posts related in here, but none solve my problem. All the information I need is coming right, only email and birthday is missing.

我在shtml部分页面的代码是这样的:

My code in shtml partial page is like this:

<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 控制器中的代码:

And my code in Facebook Controller:

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");
    }
}

如果我把这些行,如示例所示:

If I put these lines, as shown in the sample:

[FacebookAuthorize(Permissions = ExtendedPermissions)]

if (fbWebContext.IsAuthorized(ExtendedPermissions.Split(',')))

我的应用停止工作.

有人有解决办法吗?另一个示例代码以获得授权请求电子邮件和生日?

Anybody has a solution? O another sample codes to get authorized to request email and birthday?

编辑

问题出在这一行:

}, { scope: '@ViewBag.ExtendedPermissions' });

在我的应用中应该是:

}, { scope: 'email,user_birthday,user_hometown' }); //alter to get permission to access user data

我没有为 facebook 访问设置范围(权限)...但现在,我有另一个问题.

I didn't set a scope (perms) to facebook access... but now, I have another question.

我的文化 DateTime 是 dd/MM/yyyy 并且 Facebook 生日设置为 MM/dd/yyyy,我试过这个:

My culture DateTime is dd/MM/yyyy and Facebook birthday is set up as MM/dd/yyyy, I tried this:

var birthdate = DateTime.Parse(me.birthday, CultureInfo.CreateSpecificCulture("pt-BR"));

因为我在巴西的文化,但我仍然收到 DateTime 错误:

Cuz my culture in Brazilian, but I still get DateTime error:

String was not recognized as a valid DateTime.

有人知道怎么解决吗?

推荐答案

这条线让 Facebook 生日一切正常

This line make everything works fine with Facebook Birthday

var birthdate = DateTime.ParseExact(me.birthday, "MM/dd/yyyy", CultureInfo.InvariantCulture);

这篇关于Facebook C# SDK,获取用户电子邮件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:56