Hi everyone!
As a part of my authentication system in my web gl app, I need to request the Squarespace API to check for orders to confirm that the current user has purchased a subscription.
I am currently doing this by doing a web request to the API endpoint by adding a header with the API key.
This then should return a JSON which I can format into the data I need.
This works perfectly on windows & editor builds, however when deploying the WebGL app and hosting it on AWS (embedded in my Squarespace website), I get this error:
Access to fetch at 'https://api.squarespace.com/1.0/commerce/orders?cursor=' from origin 'https://3dtinyhousebuilderfreev1.s3.ap-southeast-2.amazonaws.com' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I am aware that the response needs to have the “Access-Control-Allow-Origin” header, however, I am unsure how I can change that, as I do not own the Squarespace API server, hence not being able to change any settings.
Here is the code handling the request:
//----Communicate with server and get squarespace data-----//
string apiRequestURL = "https://api.squarespace.com/1.0/commerce/orders?cursor=";
var request = new UnityWebRequest(apiRequestURL, "0");
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer APIKEY");// My code actually contains the API key, I just can't display it here ;)
//request.SetRequestHeader("Access-Control-Allow-Origin", "*"); - I tried this, this did not make a difference.
yield return request.SendWebRequest();//Returns the result of the request
Dictionary<string, object> downloadedData;
downloadedData = new(JsonConvert.DeserializeObject<Dictionary<string, object>>(request.downloadHandler.text));//Turns data into the Dictionary downloadedData
If you want to replicate the error, load my development build here: https://www.tinyeasy.co.nz/bugtest-3d-tiny-house-designer
The error appears as soon as you try login/signup.
I am not sure where to start with this, so any help is very appreciated!!!