I’m trying to download the archive of a geojson file from cesium ion, using the following unitywebrequest:
string archiveUrl = $"https://api.cesium.com/v1/assets/{assetID}/archives/{archiveID}/download";
UnityWebRequest archive = UnityWebRequest.Get(archiveUrl);
string accessToken = "token";
archive.SetRequestHeader("Authorization", "Bearer " + accessToken);
yield return archive.SendWebRequest();
byte[] assetData = archive.downloadHandler.data;
File.WriteAllBytes("Assets/DownloadedFiles/asset_archive.txt", assetData);
I receive the following bad request error:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidArgument</Code><Message>Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified</Message><ArgumentName>Authorization</ArgumentName><ArgumentValue>Bearer token</ArgumentValue><RequestId>*******</RequestId><HostId>*****</HostId></Error>
From the description, it sounds as though I shouldn’t be setting my ‘authorization’ header, but removing this only creates an invalid credentials issue.
I’m unfamiliar with UnityWebRequest
, but the archiveURL
will return a '302 Found" redirect and it’s not clear to me that UnityWebRequest
is following the redirect correctly. My guess is that it is incorrectly including the Authorization header when following the redirect, which would be a bug on the UnityWebRequest
end (unless there is a way to configure this behavior).
Another possible workaround would be if there is a way to tell UnityWebRequest
not to automatically follow the redirect and then you can follow it yourself by getting the Location
header from the original response.
Thanks for the response, Matt.
As best I can tell, I’m not getting a redirect at all, and I didn’t even realize that the request WOULD redirect (Ion REST API documentation – Cesium doesn’t mention this behaviour). Could you please write an example of a cURL request (or whatever technology you’re familiar with) to download an archive that I could model the unitywebrequest from?
Ok, I’ve got around the problem. An equivalent cURL request was working fine, so I switched to using an httpwebrequest:
async Task sendRequest(string url)
{
using (HttpClient client = new HttpClient())
{
// Set authorization header
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + IONAccessToken);
try
{
// Make the request
HttpResponseMessage response = await client.GetAsync(new Uri(url));
// Check if the request was successful
if (response.IsSuccessStatusCode)
{
// Save the downloaded data to a file
byte[] content = await response.Content.ReadAsByteArrayAsync();
File.WriteAllBytes("Assets/output.zip", content);
}
else
{
Debug.LogError("Error during download. Status code: " + response.StatusCode);
}
}
catch (Exception e)
{
Debug.LogError("Exception during download: " + e.Message);
}
}
}
I think this might just be an issue with UnityWebRequests.