无法通过FacebookClient

时间:2017-10-03 14:43:34

标签: facebook-c#-sdk

我正在尝试使用以下代码登录Facebook,但无法获取用户的电子邮件地址。我错过了什么?

这是请求asp.net mvc动作方法,它返回FacebookOAuthResult:

public ActionResult FacebookStartLogin(string returnUrl)
    {
        if (!this.Config.AuthenticationProviders.Facebook.IsDefined)
        {
            return ResultHelper.ForbiddenResult(this);
        }
        var oAuthClient = new FacebookOAuthClient();
        oAuthClient.AppId = this.Config.AuthenticationProviders.Facebook.ApiKey;
        oAuthClient.RedirectUri = new Uri(Request.Url, Url.Action("FacebookFinishLogin", "Authentication"));
        var loginUri = oAuthClient.GetLoginUrl(new Dictionary<string, object>() { { "state", Session.SessionToken } });

        Session.NextUrl = returnUrl;

        return new RedirectResult(loginUri.AbsoluteUri);
    }

以下操作方法尝试检索用户详细信息但无法获取用户电子邮件地址

public ActionResult FacebookFinishLogin(string code, string state)
    {
        if (!this.Config.AuthenticationProviders.Facebook.IsDefined)
        {
            return ResultHelper.ForbiddenResult(this);
        }
        FacebookOAuthResult oauthResult;
        if (state == Session.SessionToken && FacebookOAuthResult.TryParse(Request.Url, out oauthResult))
        {
            if (oauthResult.IsSuccess)
            {
                FacebookClient fbClient = new FacebookClient();
                dynamic result = fbClient.Post("oauth/access_token", new
                {
                    client_id = this.Config.AuthenticationProviders.Facebook.ApiKey,
                    client_secret = this.Config.AuthenticationProviders.Facebook.SecretKey,
                    redirect_uri = new Uri(Request.Url, Url.Action("FacebookFinishLogin", "Authentication")),
                    code = code
                });

                fbClient.AccessToken = result.access_token;

                dynamic facebookUser = fbClient.Get("me?fields=id,name,first_name,last_name,about,link,birthday,timezone,email");

                User user = _service.GetByProviderId(AuthenticationProvider.Facebook, facebookUser.id);
                if (user == null)
                {
                    //Its a new user for the application
                    user = SecurityHelper.CreateUser(facebookUser);
                    user = _service.Add(user, AuthenticationProvider.Facebook, facebookUser.id);
                }

                //Log the user in
                Session.User = new UserState(user, AuthenticationProvider.Facebook);

                if (!User.MobileNoVerified)
                    return Redirect(Url.Action("VerifyMobile", "Users"));

                return Redirect(Session.NextUrl);
            }
            return ResultHelper.ForbiddenResult(this);
        }
        else
            return ResultHelper.ForbiddenResult(this);
    }

作为回应,我们可以看到FB共享的其他参数,但不是电子邮件。这是调试输出图像: Debug output

1 个答案:

答案 0 :(得分:0)

您的代码似乎是正确的。确保您从facebook http://developers.facebook.com/docs/authentication/permissions/

请求适当的权限

示例:https://gist.github.com/ntotten/4341111