3

Something changed from facebook APIs v2.3 to v2.4 - after using 2.4 I can only get name and id,

I saw some posts talking about adding a "?fields=scopes..." but it's a true mystery for me - I'm trying to understand how to send these scope fields...

Here is my code:

        var facebookAuthOptions = new FacebookAuthenticationOptions();

        facebookAuthOptions.AppId = facebookAuthAppId;
        facebookAuthOptions.AppSecret = facebookAuthAppSecret;

        facebookAuthOptions.Scope.Add("email");
        facebookAuthOptions.Scope.Add("user_birthday");
        facebookAuthOptions.Scope.Add("public_profile");

        facebookAuthOptions.Provider = new FacebookAuthenticationProvider()
        {
            OnAuthenticated = (context) =>
            {
                // https://stackoverflow.com/questions/25966530/asp-net-mvc-5-1-c-sharp-owin-facebook-authentication-or-login-ask-for-birthday/25967936#25967936
                context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
                foreach (var claim in context.User)
                {
                    var claimType = string.Format("urn:facebook:{0}", claim.Key);
                    var claimValue = claim.Value.ToString();
                    if (!context.Identity.HasClaim(claimType, claimValue))
                        context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
                }

                return Task.FromResult(0);
            }
        };
        app.UseFacebookAuthentication(facebookAuthOptions);

Thanks in advance.

UPDATE 1: I tried to inject the scope fields like this:

facebookAuthOptions.UserInformationEndpoint += "?fields=email,user_birthday,first_name,last_name";

But still not working...

UPDATE 2: I found this post that makes it possible to get the email, but I have to install additional Facebook SDK for .NET which I prefer to avoid... also found this post that shows how to extract more values like birthday (in facebook APIs v4.3 I had to send birthday and not user_birthday) dynamic myInfo = fb.Get("/me?fields=email,birthday"); I'm still looking for a way to send the scope values through the standard Oauth2 methods...

UPDATE 3: I managed to get more info using the Facebook .NET SDK - https://stackoverflow.com/a/31933544/3187389 But still prefer not be able to do that with the standard Oauth2 (and Owin) without using Facebook .NET SDK...

Community
  • 1
  • 1
Yovav
  • 2,557
  • 2
  • 32
  • 53

2 Answers2

1

Facebook made some changes in 2.4. You only get those two fields by default now, if you want any other fields you have to explicitly request them:

Declarative Fields To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.

https://developers.facebook.com/docs/apps/changelog#v2_4_changes

Unfortunately I'm not sure how to translate that for the Library that you're using.

Keab42
  • 688
  • 13
  • 28
  • Hi. I'm using the latest version 3.0.1 of the default Microsoft.Owin.Security (Microsoft.Owin.Security.Facebook), if I have to use a custom SDK then it defeats the purpose of using Oauth2 - I'm very interested in finding a solution for this... – Yovav Aug 10 '15 at 07:44
  • I think your idea of injecting the scopes is along the right lines. Ultimately you'll be making a GET request, and they are just appended as querystring variables. I think it's going to depend on the exact value of that UserInformationEndpoint string though. If that string already has a "?" then you might need to just append with a leading "&" – Keab42 Aug 11 '15 at 15:29
  • 1
    @Yovav, see [this](http://stackoverflow.com/questions/32059384/why-new-fb-api-2-4-returns-null-email-on-mvc-5-with-identity-and-oauth-2). It's an 100% OAuth solution. – Douglas Gandini Sep 24 '15 at 16:03
1

SOLVED! finally... and working with the new facebook APIs v2.4

So maybe I can save someone else 6 hours :-)

You will need to install Facebook SDK for .NET

This is how I fixed it:

// Use Facebook SDK for .NET to get more specific data (https://github.com/facebook-csharp-sdk/facebook-csharp-sdk)

var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
var facebookAccessToken = identity.FindFirstValue("FacebookAccessToken");
var fb = new FacebookClient(facebookAccessToken);

dynamic facebookInfo = fb.Get("/me?appsecret_proof=YourFacebookAppSecretProof&fields=email,birthday,gender");
signInInfo.Email = facebookInfo.email;

UPDATE: it is now also required to pass in the appsecret_proof (added to the snippet)

Yovav
  • 2,557
  • 2
  • 32
  • 53
  • Hi, you can do it anywhere, I put it in my account controller inside the method that handles to new signup with an external provider. – Yovav Mar 29 '17 at 00:37