【发布时间】: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