Welcome friends to leave messages. But please do not send advertising information, it is useless. I don't have money to pay

开发使用Google账户登录功能的坑

10/25/2023 4:40:21 PM
901
0

说明:使用netcore 框架开发时会遇到这个问题,其他语言的不会存在这个类型的错误,其他语言请移步

项目是基于net7进行开发,在使用google的“一键登录”功能时前面进行的很顺利,触发登录功能后能跳转到google 的登录授权页面,并进行授权。

问题出在授权完毕后,跳转到回调页面。页面报告如下错误

An unhandled exception occurred while processing the request.
TaskCanceledException: A task was canceled.

System.Threading.Tasks.TaskCompletionSourceWithCancellation<T>.WaitWithCancellationAsync(CancellationToken cancellationToken)

TimeoutException: A task was canceled.

System.Threading.Tasks.TaskCompletionSourceWithCancellation<T>.WaitWithCancellationAsync(CancellationToken cancellationToken)

TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.

System.Net.Http.HttpClient.HandleFailure(Exception e, bool telemetryStarted, HttpResponseMessage response, CancellationTokenSource cts, CancellationToken cancellationToken, CancellationTokenSource pendingRequestsCts)

Exception: An error was encountered while handling the remote login.

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()

在这个错误出现后各种百度和Google搜索,Google文档翻来覆去的看。结果百思不得骑姐。

最后无奈扒拉相关的源代码。根据堆栈返回内容找到问题所在方法:Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler<TOptions>.ExchangeCodeAsync

TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.
System.Net.Http.HttpClient.HandleFailure(Exception e, bool telemetryStarted, HttpResponseMessage response, CancellationTokenSource cts, CancellationToken cancellationToken, CancellationTokenSource pendingRequestsCts)
System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler<TOptions>.ExchangeCodeAsync(OAuthCodeExchangeContext context)
Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler<TOptions>.HandleRemoteAuthenticateAsync()
Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()

找到源码中的这句(看图中红色画线)

很显然,这个http请求任务出了问题。TokenEndpoint 这个地址没有请求到。可是我把它放到浏览器明明可以访问。没错,它时Google 的api 终结点。当我再次徜徉在google的搜索海洋中的时候,突然灵光一闪:mmp的我浏览器开了代理,但是系统没有开代理。所以在我打开了操作系统vpn后这个错误果然消失了。

总结:mmp

 

全部评论



提问