I am trying to set up a Unity Web Request to be able to export my custom analytics but run into a Bad Request error when trying to get it. I have looked through my code and everything seems to be set up correctly.
private const string PROJECT_ID = "*********************"; // Find this in your Operate dash board/Overview/UPID
private const string API_KEY = "********************"; // Find this in your Operate dash board/Settings/Analytics Settings/Project Secret Key
private const string BASE_URL = "https://analytics.cloud.unity3d.com";
private void Start()
{
CreateExport("2020-03-09", "2020-04-08", "custom");
}
private void CreateExport(string startDate, string endDate, string dataSet)
{
Debug.Log("Start create export request.");
string postUrl = BASE_URL + "/api/v2/projects/" + PROJECT_ID + "/rawdataexports";
Dictionary<string, object> dataDictionary = new Dictionary<string, object>();
dataDictionary.Add("startDate", startDate);
dataDictionary.Add("endDate", endDate);
dataDictionary.Add("format", "json");
dataDictionary.Add("dataset", dataSet);
string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(dataDictionary);
Debug.Log(jsonData);
UnityWebRequest webRequest = UnityWebRequest.Post(postUrl, jsonData);
webRequest.SetRequestHeader("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(PROJECT_ID + ":" + API_KEY)));
webRequest.SetRequestHeader("Content-Type", "application/json");
StartCoroutine(WaitForRequest(webRequest));
}
private IEnumerator WaitForRequest(UnityWebRequest webRequest)
{
yield return webRequest.SendWebRequest();
// check for errors
if (webRequest.error == null)
{
Debug.Log("UnityWebRequest Okay!: " + webRequest.downloadHandler.text);
ReceivedData receivedData = JsonConvert.DeserializeObject<ReceivedData>(webRequest.downloadHandler.text);
ProcessRequest(receivedData);
}
else
{
Debug.LogError("UnityWebRequest Error: " + webRequest.error);
}
}
I have checked to make sure that my data I am sending is being serialized correctly and it comes out raw as:
“{"startDate":"2020-03-09","endDate":"2020-04-08","format":"json","dataset":"custom"}” which looks correct as well.
Any help would be greatly appreciated. Any clarification needed by me just ask.