首页 > 解决方案 > 使用 ASP.NET Core 设置 Twitter 外部登录时出错

问题描述

我有一个 ASP.Net Core 2.2 应用程序,并且 twitter 授权已开始显示Response status code does not indicate success: 403 (Forbidden)错误。我已经三次检查了 ClientId、ClientSecret 以及网站 url、回调 url、条款和条件 url、隐私政策 url,一切似乎都很好(请参见下面的屏幕截图)。关于我的下一步应该是什么的任何输入?

这里的文档说"The request is understood, but it has been refused or access is not allowed. An accompanying error message will explain why. This code is used when requests are being denied due to update limits . Other reasons for this status being returned are listed alongside the error codes in the table below."

当 ASP.Net Core twitter 身份验证与 Twitter 通信时,如何捕获错误代码(以及 HTTP 状态代码(在本例中为 403))?

启动.cs

 public void ConfigureServices(IServiceCollection services)
    {
 #region Add Twitter Authentication
        .AddTwitter(twitterOptions =>
        {
          twitterOptions.ConsumerKey = Configuration["TwitterAuthSettings:ClientId"];
          twitterOptions.ConsumerSecret = Configuration["TwitterAuthSettings:ClientSecret"];
          twitterOptions.SaveTokens = true;
          twitterOptions.Events.OnCreatingTicket = oAuthCreatingTicketContext =>
           {
             var authenticationTokens = oAuthCreatingTicketContext.Properties.GetTokens().ToList();
             var authenticationToken = new AuthenticationToken()
             {
               Name = "TicketCreated",
               Value = DateTime.UtcNow.ToString()
             };
             authenticationTokens.Add(authenticationToken);
             oAuthCreatingTicketContext.Properties.StoreTokens(authenticationTokens);
             return Task.CompletedTask;
           };
          twitterOptions.Events.OnTicketReceived = ticketReceivedContext =>
           {
             return Task.CompletedTask;
           };
          twitterOptions.Events.OnRemoteFailure = remoteFailureContext =>
           {
             return Task.CompletedTask;
           };
        })
      #endregion Add Twitter Authentication
}

堆栈跟踪:

Exception occured: System.Net.Http.HttpRequestException: Response status code does not indicate success: 403 (Forbidden).    
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()    at Scrubber.ExceptionMiddleware.InvokeAsync(HttpContext httpContext) in /src/Scrubber/ExceptionMiddleware.cs:line 25

在此处输入图像描述

标签: c#asp.net-coretwitterasp.net-authentication

解决方案


推荐阅读