Search Unity

Google Cloud CORS WebGL

Discussion in 'Editor & General Support' started by domdev, Jan 31, 2020.

  1. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    I'm geting error from cors when building webGL, working in POST Request here my code
    , Works okay in unity EDITOR but not in WebGL build
    also we already added the access controls need to add

    "Access-Control-Allow-Credentials": "true", "Access-Control-Allow-Headers": "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Origin": "*",

    Code (CSharp):
    1. var uwr = new UnityWebRequest(url, "POST");
    2. byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
    3. uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
    4. uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
    5. uwr.SetRequestHeader("Content-Type", "application/json");
    6. uwr.SetRequestHeader("Cache-Control", "no-cache");
    7. uwr.SetRequestHeader("Pragma", "no-cache");
    8. uwr.SetRequestHeader("Accept", "*/*");
    9.  
    10. uwr.useHttpContinue = false;
    11.  
    12. yield return uwr.SendWebRequest();
    here the error
    upload_2020-1-31_21-18-57.png


    and here using the POSTMAN

    upload_2020-1-31_21-19-18.png
     

    Attached Files:

  2. jaimelugo

    jaimelugo

    Joined:
    Nov 8, 2019
    Posts:
    27
    Hey! - I am not 100% sure if this will solve your issue, I was having the CORS error while trying to access a video in google cloud from my unity WEBGL build

    the solution I found was entirely in the GoogleCloud Console, two steps below.

    First, I change the cors setting in the google bucket. - this link below explained how to do it...
    https://bitmovin.com/docs/encoding/faqs/how-do-i-set-up-cors-for-my-google-cloud-storage-bucket

    In addition you have to make your bucket public
    https://glaforge.appspot.com/article/tip-making-a-google-cloud-storage-bucket-or-file-public

    I did everything in the cloud shell command... It takes a bit of time to get a hold on it but is super easy afterwards

    Once this is done, the webgl build worked perfectly. Have in mind that this method will open the content for any domain request.

    Hope this helps!
     
    Saint_Taint likes this.
  3. Saint_Taint

    Saint_Taint

    Joined:
    Jan 8, 2021
    Posts:
    1
    You have saved me this solved my CORs error as well--got my URL videos running from the google bucket now!
     
  4. mesmes123

    mesmes123

    Joined:
    Oct 11, 2021
    Posts:
    9
    Thank you man ! it works perfectly when trying to load assets from google storage bucket to my unity webGL build stored as well on the google cloud
     
  5. StudioZ7

    StudioZ7

    Joined:
    Nov 13, 2021
    Posts:
    10

    You saved my life!
     
    jaimelugo likes this.
  6. Gord10

    Gord10

    Joined:
    Mar 27, 2013
    Posts:
    142
    I am also having that problem. My purpose is to host a server that will keep OpenAI secret key and connects the client to OpenAI. That Google Cloud server works perfectly for standalone, but not in WebGL.

    I know a solution is posted here, but I'm not entirely sure if the confidential content (i.e., secret key) would still be hidden from the public if I followed that solution.