谷歌OAuth ASP.NET MVC示例代码



我使用此代码示例Web应用程序(ASP.NET MVC(访问Google Classroom API。

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

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

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

我刚刚将我的控制器从HomeController重命名为GoogleClassroomController,所以我的按钮实际上调用/GoogleClassroom/IndexAsync

在IndexAsyncelse中,方法的一部分被执行

其他部分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源代码将允许我进行此调用。

修复了它!

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

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

现在点击按钮,我写了CCD_;使用谷歌登录";流程工作正常

最新更新