I'm trying to use the Google Classroom API through my AWS EC2 web app (PHP-based), but I keep running into a CORS error.
I'm following this tutorial to try and request a new scope: https://developers.google.com/api-client-library/php/auth/web-app
When a user clicks a button, I make an AJAX request to my server to gather the required details and then call
$auth_url = $client->createAuthUrl();
header('Access-Control-Allow-Origin: *');
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL),false);
But I get the following error in my browser console
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.
I'm not really familiar with CORS beyond knowing what it is, so I would appreciate any suggestions on what I need to do to fix this.