DotNetOpenAuth Get Facebook Email Address
Asked Answered
C

2

10

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");
Categorize answered 18/3, 2011 at 18:39 Comment(0)
C
11

Add the following bits:

            var scope = new List<string>();
            scope.Add("email");
            fbClient.RequestUserAuthorization(scope);
Categorize answered 18/3, 2011 at 18:54 Comment(2)
yep - scope are facebook permissionsCategorize
Note that in newer version of DotNetOpenAuth.ApplicationBlock, ClientSecret doesn't exist. It should now be as follows: FacebookClient fbClient = new FacebookClient { ClientIdentifier = "appId", ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("appSecret") };Luxuriate
D
1

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.

Dairying answered 7/3, 2013 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.