I am just trying to access files stored on firebase storage using a download URL I received using a plugin.
The url itself works great on the browser & in the editor using the method below. However when building to WebGL and hosting on AWS, unity seems to adjust the URL
GET https://3dtinyhousebuilderfreev1.s3.ap-southeast-2.amazonaws.com/Build+67+D+-+Cloud+Business+Settings/%22https://firebasestorage.googleapis.com/v0/b/d-tiny-house-designer.appspot.com/o/BusinessLogos%2FTinyEasy.png?alt=media&token=dc660593-e7d1-4c99-bd3e-27dcb72f708e%22 403 (Forbidden)
Here is my code:
//string url = https://firebasestorage.googleapis.com/v0/b/d-tiny-house-designer.appspot.com/o/BusinessLogos%2FTinyEasy.png?alt=media&token=dc660593-e7d1-4c99-bd3e-27dcb72f708e
public static async Task<Texture2D> GetRemoteTexture(string url)
{
using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(url))
{
Debug.Log(url);
www.url = url;
//www.uri = new System.Uri(url); <- system.uri using the url simply stops the execution without errors.
// begin request:
await www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log($"{www.error}, URL:{www.url}");
return null;
}
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log($"{www.error}, URL:{www.url}");
return null;
}
if (www.result == UnityWebRequest.Result.Success)
{
var result = DownloadHandlerTexture.GetContent(www);
Debug.Log("Result: " + result);
return result;
}
return null;
}
}
What can I do to avoid this and access the file successfully?
The firebase storage settings have been set to read and write without authentication, and this can not be the problem as it works in the editor anyways.
Good point! I have been using async/await with WebGL successfully for all other tasks in the app so far, but will see if that will have an impact. Taking that I am getting a response with a 403 error and the rest of the code executes, I don’t think this can be the cause of the issue? I will test this, though!
I am doing two other web requests, which successfully request data from the Squarespace & Mailchimp API. I had to re-route these through a proxy due to CORS issues when sending a web request from the hosted web app directly. I am not sure if the proxy mitigates the prefix issue. I have tried implementing the same proxy method also for this request, however am getting a 400 bad request error even in unity editor so have not investigated this further.
This is the method I am using to successfully access the Squarespace API through my proxy with a GET request:
public IEnumerator GetSquarespaceAPI()
{
string apiRequestURL = "https://api.squarespace.com/1.0/commerce/orders?cursor=";
string corsProxyUrl = "https://*hiddenproxy*.herokuapp.com/"; //Had to hide the actual proxy url.
var getRequest = CreateRequest(corsProxyUrl + apiRequestURL);// Gets the order information from Squarespace
AttachHeader(getRequest, "Origin", "https://3dtinyhousebuilderfreev1.s3.ap-southeast-2.amazonaws.com");
AttachHeader(getRequest, "Authorization", "Bearer *API KEY HIDDEN*");
//Attaches the Authorization API Key to the Header
//Had to hide the API key for obvious reasons ;)
yield return getRequest.SendWebRequest();//Returns the result of the request
Dictionary<string, object> downloadedData = new();
try
{
downloadedData = new(JsonConvert.DeserializeObject<Dictionary<string, object>>(getRequest.downloadHandler.text));//Turns data into the Dictionary downloadedData
}
catch(Exception exceptionMessage)
{
ErrorMessagePopup.Instance.DisplayError(exceptionMessage);
}
}
private UnityWebRequest CreateRequest(string path, RequestType type = RequestType.GET, object data = null) //This does the magic of the request behind the scene
{
var request = new UnityWebRequest(path, type.ToString());
if (data != null)
{
var bodyRaw = Encoding.UTF8.GetBytes(JsonUtility.ToJson(data));
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
}
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
return request;
}
private void AttachHeader(UnityWebRequest request, string key, string value) //Attaches a header to the request for authorization
{
request.SetRequestHeader(key, value);
}
public enum RequestType //Changes the request type from numbers to easier to understand GET,POST and PUT
{
GET = 0,
POST = 1,
PUT = 2
}