【问题标题】:Version Deprecation Facebook Graph API v2.2版本弃用 Facebook Graph API v2.2
【发布时间】:2017-08-20 20:46:58
【问题描述】:

我们的 Facebook 登录现在无法正常工作。我们收到了来自 Facebook 开发者门户的消息:

“应用程序名称”当前可以访问 Graph API v2.2,该 API 将到达其末尾 2017 年 3 月 27 日的 2 年生命周期。为确保顺利过渡, 请将所有调用迁移到 Graph API v2.3 或更高版本。

要检查您的应用是否会受到此次升级的影响,您可以使用 版本升级工具。这将向您显示哪些呼叫(如果有)是 受此更改以及较新版本中的任何替换调用的影响 版本。如果您没有看到任何呼叫,您的应用可能不会受到影响 这种变化。

您还可以使用我们的变更日志查看所有变更的完整列表 图形 API 版本。

我们正在使用 ASP.NET MVC 5,并且我们正在使用或身份验证是这样的:

var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
            {
                AppId = "****",
                AppSecret = "****",
                AuthenticationType = "Facebook",
                SignInAsAuthenticationType = "ExternalCookie",
                Provider = new FacebookAuthenticationProvider
                {
                    OnAuthenticated = async ctx => ctx.Identity.AddClaim(new Claim(ClaimTypes.Email, ctx.User["email"].ToString()))
                }
            };

            facebookAuthenticationOptions.Scope.Add("email");

但是今天,我们的登录信息对象在 ExternalLoginCallback 中为空:

        [HttpGet]
        [AllowAnonymous]
        [RequireHttps]
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl = null)
        {
            try
            {
                var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (loginInfo == null)
                {
                    return RedirectToAction("Login");
                }
... more code here...

在 Facebook 开发中。 Portal我们的API版本是2.3

我们测试了许多选项,但没有结果:

Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5

Why new fb api 2.4 returns null email on MVC 5 with Identity and oauth 2?

非常感谢您的帮助。

【问题讨论】:

标签: asp.net-mvc facebook facebook-graph-api asp.net-mvc-5


【解决方案1】:

我遇到了同样的问题,这是我设法解决它并从 Facebook 获取电子邮件的方法。

  • 在 NuGet 包之后更新
    • Microsoft.Owin 到版本 3.1.0-rc1
    • Microsoft.Owin.Security 到版本 3.1.0-rc1
    • Microsoft.Owin.Security.Cookies 到版本 3.1.0-rc1
    • Microsoft.Owin.Security.OAuth 到版本 3.1.0-rc1
    • Microsoft.Owin.Security.Facebook 到版本 3.1.0-rc1

然后在Identity Startup类中加入如下代码

var facebookOptions = new FacebookAuthenticationOptions()
        {
            AppId = "your app id",
            AppSecret = "your app secret",
            BackchannelHttpHandler = new FacebookBackChannelHandler(),
            UserInformationEndpoint = "https://graph.facebook.com/v2.8/me?fields=id,name,email,first_name,last_name",
            Scope = { "email" }
        };

        app.UseFacebookAuthentication(facebookOptions);

这是FacebookBackChannelHandler()的定义类:

using System;
using System.Net.Http;

public class FacebookBackChannelHandler : HttpClientHandler
{
    protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        System.Threading.CancellationToken cancellationToken)
    {
        // Replace the RequestUri so it's not malformed
        if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
        {
            request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token"));
        }

        return await base.SendAsync(request, cancellationToken);
    }
}

【讨论】:

  • 这完全是我实施的完整解决方案,好点
  • 这太不可思议了......糟糕的糟糕 OWIN 团队......我会尝试,稍后再回复你,我的应用因此而死了
  • @user3012760 是否需要包实现?因为西班牙语的 RC1 包仍然不可用....
【解决方案2】:

只需更新所有与 OWIN 相关的参考 OWIN 最新版本是 3.1.0rc1。

这修复了登录按钮,而不是电子邮件,我无法弄清楚这些问题。

【讨论】:

  • 它修复了登录按钮,特别是因为 Facebook 已将身份验证令牌请求的响应类型更新为 JSON,而 MS Owin 3.0.1 无法处理。
  • 我已更新到 rc1 并已修复。但我需要插入一个答案的代码
  • 很好,我也会看看。
【解决方案3】:

如果您因为语言包(如我的情况)而无法更新 OWIn 包,您可以

  1. 修改身份启动类代码:

    var facebookOptions = new FacebookAuthenticationOptions()
    {
        AppId = "your app id",
        AppSecret = "your app secret",
        BackchannelHttpHandler = new FacebookBackChannelHandler(),
        UserInformationEndpoint = "https://graph.facebook.com/v2.8/me?fields=id,name,email,first_name,last_name",
        Scope = { "email" }
    };
    
    app.UseFacebookAuthentication(facebookOptions);
    
  2. 这是 FacebookBackChannelHandler() 的定义类:

    public class FacebookBackChannelHandler : HttpClientHandler
    {
        protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
            {
                request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token"));
            }
    
            var result = await base.SendAsync(request, cancellationToken);
            if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
            {
                return result;
            }
    
            var content = await result.Content.ReadAsStringAsync();
            var facebookOauthResponse = JsonConvert.DeserializeObject<FacebookOauthResponse>(content);
    
            var outgoingQueryString = HttpUtility.ParseQueryString(string.Empty);
            outgoingQueryString.Add(nameof(facebookOauthResponse.access_token), facebookOauthResponse.access_token);
            outgoingQueryString.Add(nameof(facebookOauthResponse.expires_in), facebookOauthResponse.expires_in + string.Empty);
            outgoingQueryString.Add(nameof(facebookOauthResponse.token_type), facebookOauthResponse.token_type);
            var postdata = outgoingQueryString.ToString();
    
            var modifiedResult = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(postdata)
            };
    
            return modifiedResult;
        }
    }
    
    private class FacebookOauthResponse
    {
        public string access_token { get; set; }
        public long expires_in { get; set; }
        public string token_type { get; set; }
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多