【问题标题】:CORS Issue with Google OAuth ASP.NET MVC sample codeGoogle OAuth ASP.NET MVC 示例代码的 CORS 问题
【发布时间】:2021-04-06 08:54:09
【问题描述】:

我正在使用此代码示例 Web applications (ASP.NET MVC) 访问 Google Classroom API。

我已将这些添加到 Google Cloud Platform 项目中。

  • http://localhost:51234 作为授权的 JavaScript 来源
  • http://localhost:51234/AuthCallback/IndexAsync 授权 重定向 URI

我网页中的一个按钮调用上面示例代码链接中给出的/Home/IndexAsync

我刚刚将控制器从 HomeController 重命名为 GoogleClassroomController,所以我的按钮实际上调用了 /GoogleClassroom/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.
GET <above_URL> net::ERR_FAILED

我做错了什么?

我们不能从http://localhost:51234调用https://accounts.google.com/o/oauth2/v2/auth吗?

我认为将 http://localhost:51234 添加为云项目中的授权 JavaScript 来源可以让我进行此调用。

【问题讨论】:

    标签: c# asp.net-mvc google-oauth google-api-dotnet-client


    【解决方案1】:

    修好了!

    我的按钮实际上调用了/GoogleClassroom/IndexAsync

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

    现在点击按钮,我写了window.location.href = "/GoogleClassroom/IndexAsync";,整个“使用 Google 登录”流程正常运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 2013-03-03
      • 2016-03-23
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多