C# CORS问题与Google OAuth ASP.NET MVC示例代码

C# CORS问题与Google OAuth ASP.NET MVC示例代码,c#,asp.net-mvc,google-oauth,google-api-dotnet-client,C#,Asp.net Mvc,Google Oauth,Google Api Dotnet Client,我使用这个代码示例来访问Google教室API 我在谷歌云平台项目中添加了这些 http://localhost:51234作为授权的JavaScript源代码 http://localhost:51234/AuthCallback/IndexAsync经授权 重定向URI 我的网页中的一个按钮调用上述示例代码链接中给出的/Home/IndexAsync 我刚刚将我的控制器从HomeController重命名为GoogleClassroomController,因此我的按钮实际上调用了/Go

我使用这个代码示例来访问Google教室API

我在谷歌云平台项目中添加了这些

  • http://localhost:51234
    作为授权的JavaScript源代码
  • http://localhost:51234/AuthCallback/IndexAsync
    经授权 重定向URI
我的网页中的一个按钮调用上述示例代码链接中给出的
/Home/IndexAsync

我刚刚将我的控制器从
HomeController
重命名为
GoogleClassroomController
,因此我的按钮实际上调用了
/Google教室/IndexAsync

在IndexAsync
else
中,执行部分方法

else部分中的
result.RedirectUri
的值为

https://accounts.google.com/o/oauth2/v2/auth?
    access_type=offline&
    response_type=code&
    client_id=abc.apps.googleusercontent.com&
    redirect_uri=http%3A%2F%2Flocalhost%3A51234%2FAuthCallback%2FIndexAsync&
    scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclassroom.courses.readonly&
    state=http%3A%2F%2Flocalhost%3A51234%2FGoogleClassroom%2FIndexAsync
在浏览器控制台中,我得到2个错误

Access to XMLHttpRequest at 
'<above_URL>' 
(redirected from 'http://localhost:51234/GoogleClassroom/IndexAsync')
from origin 'http://localhost:51234' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
访问位于的XMLHttpRequest
'' 
(从'http://localhost:51234/GoogleClassroom/IndexAsync')
源于http://localhost:51234'已被CORS策略阻止:
对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。
GET net::ERR\u失败
我做错了什么

不允许我们呼叫
https://accounts.google.com/o/oauth2/v2/auth
from
http://localhost:51234

我想添加
http://localhost:51234
因为云项目中授权的JavaScript源代码允许我打这个电话。

修复了它

我的按钮实际上调用了
/google教室/IndexAsync

我们使用一个自定义函数来进行这些调用。在进一步调查之后,我发现这个自定义函数在内部使用了
$.ajax()

现在点击按钮,我写下了
window.location.href=“/google教室/IndexAsync”
和整个“使用Google登录”流程正常工作

GET <above_URL> net::ERR_FAILED