Facebook v2.4与OAuth2停止工作,只获取名称和ID [英] Facebook v2.4 with OAuth2 stopped working and only getting name and id

查看:147
本文介绍了Facebook v2.4与OAuth2停止工作,只获取名称和ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些东西从facebook API v2.3更改为v2.4 - 使用2.4后我只能获取名称和ID,



我看到一些帖子谈论添加?fields = scopes ...但这对我来说真是个谜 - 我想知道如何发送这些范围字段...



这是我的代码:

  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 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)) ;
}

返回Task.FromResult(0);
}
};
app.UseFacebookAuthentication(facebookAuthOptions);

提前感谢



更新1 :我试图注入这样的范围字段:

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

但仍然不工作...



更新2:我发现这篇文章可以获得电子邮件,但是我必须安装额外的 Facebook SDK for .NET ,我喜欢避免...还发现这篇文章,显示如何提取更多的值,如生日(在facebook API v4.3中我必须发送生日,而不是user_birthday)
动态myInfo = fb.Get(/ me?fields = email,birthday);
我还在寻找一种通过标准Oauth2方法发送范围值的方法...



更新3:
我设法使用Facebook .NET SDK获取更多信息 - https://stackoverflow.com/a/31933544/3187389
但是仍然不喜欢不使用标准的Oauth2(和Owin)而不使用Facebook .NET SDK ...

解决方案

p>解决了!最后...和新的Facebook API v2.4



所以也许我可以保存别人6个小时: - )



您将需要安装Facebook SDK for .NET



这是我如何修复:



< pre class =snippet-code-js lang-js prettyprint-override> //使用Facebook SDK for .NET获取更具体的数据(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);动态facebookInfo = fb.Get(/ me ?appsecret_proof = YourFacebookAppSecretProof& fields = email,birthday,gender); signInInfo.Email = facebookInfo.email;



更新:现在也是需要传入appsecret_proof(添加到代码段)


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

解决方案

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)

这篇关于Facebook v2.4与OAuth2停止工作,只获取名称和ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆