AWS 网络应用程序 CORS 错误

AWS web app CORS error

我正在尝试通过我的 AWS EC2 网络应用程序(基于 PHP)使用 Google 教室 API,但我将 运行 保留在 CORS 中错误。

我正在按照本教程尝试请求新范围:https://developers.google.com/api-client-library/php/auth/web-app

当用户单击按钮时,我向我的服务器发出 AJAX 请求以收集所需的详细信息,然后调用

$auth_url = $client->createAuthUrl();
header('Access-Control-Allow-Origin: *');
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL),false);

但我的浏览器控制台出现以下错误

Failed to load https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=online&client_id=xxxx.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fexample.com%2Fsubdir%2Fpage%2Fajax%2Fconfirm_classroom_auth.php&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclassroom.rosters.readonly&approval_prompt=auto&include_granted_scopes=true: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example.com' is therefore not allowed access. The response had HTTP status code 405.

除了知道它是什么之外,我对 CORS 不是很熟悉,所以如果有任何关于我需要做些什么来解决这个问题的建议,我将不胜感激。

我不太清楚为什么使用 header() 会导致错误,但我找到了解决方法。

只需将 authURL 返回到我的 AJAX 成功函数,然后调用 window.open(authURL) 就可以正常工作,没有 CORS 错误