Facebook v2.4 with OAuth2 stopped working and only getting name and id

帅比萌擦擦* 提交于 2019-12-10 10:24:58

问题


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...


回答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.




回答2:


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)



来源:https://stackoverflow.com/questions/31909920/facebook-v2-4-with-oauth2-stopped-working-and-only-getting-name-and-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!