Search Unity

Raw Data Export using UnityWebRequest causes 400 Bad Request

Discussion in 'Unity Analytics' started by JBlythDC, Apr 8, 2020.

  1. JBlythDC

    JBlythDC

    Joined:
    Apr 9, 2019
    Posts:
    3
    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.

    Code (CSharp):
    1. private const string PROJECT_ID = "*********************"; // Find this in your Operate dash board/Overview/UPID
    2.     private const string API_KEY = "********************"; // Find this in your Operate dash board/Settings/Analytics Settings/Project Secret Key
    3.     private const string BASE_URL = "https://analytics.cloud.unity3d.com";
    4.  
    5.     private void Start()
    6.     {
    7.         CreateExport("2020-03-09", "2020-04-08", "custom");
    8.     }
    9.  
    10.     private void CreateExport(string startDate, string endDate, string dataSet)
    11.     {
    12.         Debug.Log("Start create export request.");
    13.         string postUrl = BASE_URL + "/api/v2/projects/" + PROJECT_ID + "/rawdataexports";
    14.  
    15.  
    16.         Dictionary<string, object> dataDictionary = new Dictionary<string, object>();
    17.         dataDictionary.Add("startDate", startDate);
    18.         dataDictionary.Add("endDate", endDate);
    19.         dataDictionary.Add("format", "json");
    20.         dataDictionary.Add("dataset", dataSet);
    21.         string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(dataDictionary);
    22.         Debug.Log(jsonData);
    23.  
    24.         UnityWebRequest webRequest = UnityWebRequest.Post(postUrl, jsonData);
    25.         webRequest.SetRequestHeader("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(PROJECT_ID + ":" + API_KEY)));
    26.         webRequest.SetRequestHeader("Content-Type", "application/json");
    27.  
    28.         StartCoroutine(WaitForRequest(webRequest));
    29.     }
    30.  
    31.     private IEnumerator WaitForRequest(UnityWebRequest webRequest)
    32.     {
    33.         yield return webRequest.SendWebRequest();
    34.  
    35.         // check for errors
    36.         if (webRequest.error == null)
    37.         {
    38.             Debug.Log("UnityWebRequest Okay!: " + webRequest.downloadHandler.text);
    39.  
    40.             ReceivedData receivedData = JsonConvert.DeserializeObject<ReceivedData>(webRequest.downloadHandler.text);
    41.             ProcessRequest(receivedData);
    42.         }
    43.         else
    44.         {
    45.             Debug.LogError("UnityWebRequest Error: " + webRequest.error);
    46.         }
    47.     }
    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.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I would use Charles Proxy to troubleshoot this. First, get it working via curl, then compare it to your implementation. Is this for an admin app? I would not expect this in your game, every user would be unnecessarily waiting for the same data. What are you trying to accomplish? We may have other suggestions. If this is indeed for game play, you instead would want to process the export on your server, and make a real time web API available. Otherwise our servers might see this as a DOS attack if multiple users are making the same request frequently https://docs.unity3d.com/Manual/UnityAnalyticsRawDataExport.html and https://support.unity3d.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity