I tried to use Unity Cloud save services for first time as a Rest API call. I do not have a backend and wanted to use this to verify PoC projects.
I get the authentication done by using await AuthenticationService.Instance.SignInAnonymouslyAsync(); and after getting a successful response save the player id from AuthenticationService.Instance.PlayerId; in a local string.
Now, that I have this, I also have a project ID for my project with cloud save services enabled(have tested by calling CloudSaveService.Instance.Data.ForceSaveAsync).
in same project, I made a Unity POST request with the header content type set to application/json and used JsonConvert.serialize() to convert my data into a string for testing.
My main code block looks like this:
string dataJson = JsonConvert.SerializeObject(dataToSet);
UnityWebRequest request = UnityWebRequest.Post(setItemUrl, dataJson);
request.uploadHandler.contentType = "application/json";
UnityWebRequestAsyncOperation asyncOp = request.SendWebRequest();
asyncOp.completed += operation =>
{
if (request.result == UnityWebRequest.Result.Success)
{
Debug.Log("set item response code : " + request.responseCode);
}
else
{
Debug.LogError(
$"{request.responseCode} : Some error occured trying to reach
{setItemUrl}
with data as {dataJson}");
// if(request.responseCode == 401) // invalid authentication
// {
string errorData = request.downloadHandler.text;
Debug.LogError(errorData);
// }
}
};
But now, every-time I hit the request, I get
401 : Some error occured trying to reach
https://cloud-save.services.api.unity.com/v1/data/projects/ea6ad5fb-0f83-4e58-af41-c4c1d08c5d21/players/7FxEuDjJoNGHuS8SPQFzEog10gzh/items
with data as {"key":"data2","value":{"strData":"value3"},"writeLock":""}
followed by
{"status":401,"title":"Unauthorized","type":"https://services.docs.unity.com/docs/errors/#51","requestId":"c015ac2d-2cb9-4498-bd80-59b3b7a95dec","detail":"Invalid Authorization header","code":51}
Can anyone tell me what I am doing wrong?