Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DotNetOpenAuth Get Facebook Email Address

I have the following code where its grabbing First/Last name. I realize that email is an extended permission, but what would I need to modify to request extended permissions?

How do I get the email of an authenticated Facebook user through the DotNetOpenAuth?

        fbClient = new FacebookClient
        {
            ClientIdentifier = ConfigurationManager.AppSettings["facebookAppID"],
            ClientSecret = ConfigurationManager.AppSettings["facebookAppSecret"],
        };

        IAuthorizationState authorization = fbClient.ProcessUserAuthorization();
        if (authorization == null)
        {
            // Kick off authorization request
            fbClient.RequestUserAuthorization();

        }
        else
        {
            var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken));
            using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    var graph = FacebookGraph.Deserialize(responseStream);

                    // unique id for facebook based on their ID
                    FormsAuthentication.SetAuthCookie("fb-" + graph.Id, true);

                    return RedirectToAction("Index", "Admin");
                }
            }
        }

        return View("LogOn");
like image 750
aherrick Avatar asked Mar 18 '11 18:03

aherrick


2 Answers

Add the following bits:

            var scope = new List<string>();
            scope.Add("email");
            fbClient.RequestUserAuthorization(scope);
like image 181
aherrick Avatar answered Nov 03 '22 19:11

aherrick


If you are using VS2012 built in oauth providers you just need to update your oauth package. See the last post on the following link: http://forums.asp.net/t/1847724.aspx/1. The only email I can't retrieve is MS Live. Currently I use facebook, google, and yahoo.

like image 37
MarkusR Avatar answered Nov 03 '22 21:11

MarkusR