Search code examples
c#jsonamazon-web-servicesunity-game-engineamazon-cognito

AWS Cognito Auth JSON request isn't Deserializing with nested classes in Unity


My JSON request isn't Deserializing with my nested classes in Unity.

I have tried many things to figure this problem out. Can someone spot the issue?

Here is the JSON Login Request from AWS Cognito:

{
    "timestamp": "2024-05-17T02:14:10.331Z",
    "level": "INFO",
    "requestId": "2dc7b590-1827-4554-9821-908eb1d3ee3f",
    "message": {
        "$metadata": {
            "httpStatusCode": 200,
            "requestId": "0c684225-7a1b-469f-89e3-15d29da36f49",
            "attempts": 1,
            "totalRetryDelay": 0
        },
        "ChallengeName": "NEW_PASSWORD_REQUIRED",
        "ChallengeParameters": {
            "USER_ID_FOR_SRP": "348844c8-e001-7030-93c4-3c5a5a754fa2",
            "requiredAttributes": "[]",
            "userAttributes": "{\"email_verified\":\"true\",\"preferred_username\":\"RobloxHero\",\"email\":\"[email protected]\"}"
        },
        "Session": "AYABeHuZceyPXQVTVA1OPXwrdZIAHQABAAdTZXJ2aWNlABBDb2duaXRvVXNlclBvb2xzAAEAB2F3cy1rbXMAS2Fybjphd3M6a21zOnVzLWVhc3QtMTo3NDU2MjM0Njc1NTU6a2V5L2IxNTVhZmNhLWJmMjktNGVlZC1hZmQ4LWE5ZTA5MzY1M2RiZQC4AQIBAHjHL4WD3WpekpFe85nxP9Nwg99u3bPN6BTSaB-uHZcTLAF1FxlVsAaSp8ntnWxXieO7AAAAfjB8BgkqhkiG9w0BBwagbzBtAgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMjuV5Bhw0j4lWg6JxAgEQgDusJsldE8RrnX72vgmOQkw9KkrzbARBsxGkfjRqS5A6T7T71F6umtOTz7XnjEqdeAfhfyQ2vT_Hmx2-nAIAAAAADAAAEAAAAAAAAAAAAAAAAABejP7rYrS09IQqwZHglO_k_____wAAAAEAAAAAAAAAAAAAAAEAAADVlPZSlzCXntBx_E8TEbzxbn4ZTZBEr5nMSXRinXYnmVRrlKjtLX7yv8giRh1z1_rYNSKGAY2b3cz1PyWadtwifpldG6_d3NdgthTaMpQNeIhJ_jmjfz0U_4hrYWVyz5OspPbR38Rlelj5r9DIQaGWGFMVVDuzeeouxbBTl83O3w5Rx2gYYZ3AmyIBrMiVxDscytZj8oJ4fit76lVlFlFNmwMywy6MjhDb80RGcYoUpkbUZn86X1Ycy5KZstb7tm4dFp7IN1ej69IcLmYZbvcJ4xBCZeEJCrfccFbdhVG0zGcPUB1lzw"
    }
}

Here is the The Unity Plugin Code I'm making a JSON Requesting to Login:

public void LoginRequest()
    {
        AWSUserLoginRequest request = new AWSUserLoginRequest();
        request.username = username;
        request.password = password;

        using (UnityWebRequest www = UnityWebRequest.Post("https://game-api.compassoftruth.org/prod/Login", JsonUtility.ToJson(request), "application/json") )
        {
            www.SendWebRequest();
            while (www.isDone == false) {
            //do something, or nothing while blocking
            }
            if (www.result != UnityWebRequest.Result.Success) {
                Debug.Log(www.error);
            }
            else {
                LoginResult = JsonUtility.FromJson<AWSLoginResult>(www.downloadHandler.text.Replace('$'.ToString(), ""));
                // Show results as text
                Debug.Log(www.downloadHandler.text.Replace('$'.ToString(), ""));
                Debug.Log(LoginResult.level);
            }
        }
        
    }

I'm able to debug the response data in string format:

{"metadata":{"httpStatusCode":200,"requestId":"731de9a0-a3b3-4a75-a565-f19c49f19a50","attempts":1,"totalRetryDelay":0},"ChallengeName":"NEW_PASSWORD_REQUIRED","ChallengeParameters":{"USER_ID_FOR_SRP":"348844c8-e001-7030-93c4-3c5a5a754fa2","requiredAttributes":"[]","userAttributes":"{\"email_verified\":\"true\",\"preferred_username\":\"RobloxHero\",\"email\":\"[email protected]\"}"},"Session":"AYABeJlFAr90U2rURQa1RoyPy-AAHQABAAdTZXJ2aWNlABBDb2duaXRvVXNlclBvb2xzAAEAB2F3cy1rbXMAS2Fybjphd3M6a21zOnVzLWVhc3QtMTo3NDU2MjM0Njc1NTU6a2V5L2IxNTVhZmNhLWJmMjktNGVlZC1hZmQ4LWE5ZTA5MzY1M2RiZQC4AQIBAHjHL4WD3WpekpFe85nxP9Nwg99u3bPN6BTSaB-uHZcTLAH4F1Ox2jph9NZcOwFtL6m-AAAAfjB8BgkqhkiG9w0BBwagbzBtAgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQM7FLUPDofg4S5f67nAgEQgDsIOeZuVErT75jsNRBJz63Pat7GgObkPeKx7Li_z5tvnDG4FvrbLoLjVcgJpVin-rz9Cp_bcfmxyjX0owIAAAAADAAAEAAAAAAAAAAAAAAAAAAtqnLZ5SIekFGUo3n6YKYi_____wAAAAEAAAAAAAAAAAAAAAEAAADV0iUMK4jkW4ioIy8AGcb5bLTQ3jW90JXuaucWAlImLuT55a3TaWyStOL_O6uJb-2NDqz7ejkQ3eUDlMn02AYwvj75cI4bB6nuaOSomjBZ4-tkT8AtpbykIYTgc451wjg5U9CrnbbTViMR-s3Fdx6P_rABop_INWkf1lAUs1xrlY5AMX3VGc2bjlHX9Ejips9irlVpnuUXic-AJXQUcZeHbi9DmcfSFe0VDTQGWMJmLFUZFWfJAQeo-dvQadfvkm2r1Lwwn42WGVkPykxbVqdkTUxIUcpFajlRAZp5B4PRxoJCBp-CVA"}

And here is my nested classed that I'm using to try and parse the info with JSONUtility:

using System;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.VersionControl;

[Serializable]
public class AWSLoginResult
{
    public string timestamp;
    public string level;
    public string requestId;
    public message message; 

}

[Serializable]
public class message
{
    public string ChallangeName;
    public ChallangeParameters ChallangeParameters;
    public string Session;
}

[Serializable]
public class ChallangeParameters
{
    public string USER_ID_FOR_SRP;
    public string userAttributes;
    public string requiredAttributes;

}


Solution

  • My classes in C# were using the whole logs object from AWS Logs... I needed to only use the console logged object from node.js, the "message". Changing my classes to mirror the correct object looks like this.

    using System;
    using UnityEngine;
    using UnityEngine.Tilemaps;
    using UnityEngine.Networking;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEditor.VersionControl;
    using Unity.VisualScripting;
    
    [Serializable]
    public class AWSLoginResult
    {
        public metadata metadata;
        public string ChallengeName;
        public ChallangeParameters ChallangeParameters;
        public string Session;
    
    }
    
    [Serializable]
    public class metadata
    {
        public string httpStatusCode;
        public string requestId;
        public string attempts;
        public string totalRetryDelay;
    
    }
    
    [Serializable]
    public class ChallangeParameters
    {
        public string USER_ID_FOR_SRP;
        public string userAttributes;
        public string requiredAttributes;
    
    }