Facebook Graph API未返回属性user_birthday

时间:2013-12-09 15:38:25

标签: c# facebook facebook-graph-api web

我正在使用Facebook Graph API来返回我的应用的用户信息。到目前为止,我只需要将电子邮件作为扩展属性,我从来没有遇到任何问题。但是,现在我必须让用户过生日,但它没有按预期工作。我得到一个空值。

这是我用来返回信息的类的一部分

using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OAuth2;

namespace Contoso.Web.Mvc.Controls.Authentication.IdentityProviders
{
    public class FacebookIdentityProvider : IdentityProvider
    {      

        public override ActionResult Authenticate(Func<LoginParameters, ActionResult> CallbackFunction)
        {
            var authorization = fbClient.ProcessUserAuthorization();
            var urlHelper = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);

            if (authorization == null && String.IsNullOrWhiteSpace(Parameters.AccessToken))
            {
                //Kick off authorization request
                fbClient.RequestUserAuthorization(scope: new string[] { "email", "user_birthday"});
            }
            else
            {
                if(authorization != null)
                    Parameters.AccessToken = authorization.AccessToken;

                var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(Parameters.AccessToken));

                using (var response = request.GetResponse())
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        var graph = FacebookGraph.Deserialize(responseStream);
                        var user = GetUser(graph);
                        var requiresExtraInformation = false;
                        DoFormAuthenticationAndCreateUserIfNeeded(user, out requiresExtraInformation);

                        if (requiresExtraInformation)
                        {
                            return RedirectToExtraInformationPage(user);
                        }
                        return CallbackFunction(Parameters);                              
                    }
                }
            }

            return new RedirectResult(urlHelper.Action("Index"));
        }
    }

    [DataContract]
    public class FacebookGraph
    {
        private static DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(FacebookGraph));

        [DataMember(Name = "id")]
        public long Id { get; set; }

        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "first_name")]
        public string FirstName { get; set; }

        [DataMember(Name = "last_name")]
        public string LastName { get; set; }

        [DataMember(Name = "email")]
        public string Email { get; set; }

        [DataMember(Name = "gender")]
        public string Gender { get; set; }

        [DataMember(Name = "user_birthday")]
        public string UserBirthday { get; set; }

        public static FacebookGraph Deserialize(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                throw new ArgumentNullException("jsonStream");
            }

            return Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(json)));
        }

        public static FacebookGraph Deserialize(Stream jsonStream)
        {
            if (jsonStream == null)
            {
                throw new ArgumentNullException("jsonStream");
            }
            return (FacebookGraph)jsonSerializer.ReadObject(jsonStream);
        }
    }
}

当我反馈我从facebook获得的响应时,除了用户生日(它显示为null)之外,我得到了我需要的所有信息。我查了一下,我的脸谱资料确实有用户生日。

1 个答案:

答案 0 :(得分:0)

好吧,我终于找到了问题,这有点傻,但是我把它留在这里以防其他人偶然发现这个问题。

问题在于,即使我正在请求正确的范围“user_birthday”,图形对象中的属性也被称为“生日”,所以不要使用此

[DataMember(Name = "user_birthday")]
public string UserBirthday { get; set; }

我应该有这个

[DataMember(Name = "birthday")]
public string UserBirthday { get; set; }

就是这样。它现在正在运作!