JsonSerializationException: Unable to find a constructor for Auth0.AuthenticationApi.Models.DeviceCodeResponse
Hello, I posted this issue in Auth0 Community forum (see here) and it was suggested to raise an issue here as well.
We are developing a VR application using Unity, and for that I set up Auth0 inside using the guidelines here: https://github.com/auth0-lab/auth0-unity
It is already working in Unity Editor, but on the device (Android) it is throwing this exception:
2022.05.31 10:48:16.705 5710 5760 Error Unity JsonSerializationException: Unable to find a constructor to use for type Auth0.AuthenticationApi.Models.DeviceCodeResponse. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'device_code', line 1, position 15.
2022.05.31 10:48:16.705 5710 5760 Error Unity at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <00000000000000000000000000000000>:0
2022.05.31 10:48:16.705 5710 5760 Error Unity at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract contai
The problem is coming from this line of code on Unity side:
AuthManager.Instance.Auth0.ExchangeDeviceCodeAsync(clientId, deviceCodeResp.DeviceCode, deviceCodeResp.Interval);
Since the problem is coming from a class in the DLL, I can't modify it to resolve the issue. Did anybody encounter something similar and can help?
Thanks a lot!
Upon further investigation, it seems that the issue is coming from Newtonsoft.Json package. We are using Unity 2020.3 and since 2020.1+ Newtonsoft.Json is included as a Unity package with a minimum version of 12.0.0.0. The version that is included in your project is 10.0.0.0 and some DLLs that have dependencies to that version seems to be causing this issue.
These DLLs are getting affected by it : Assets/Auth0Lab/Plugins/System.IdentityModel.Tokens.Jwt.dll Assets/Auth0Lab/Plugins/Microsoft.IdentityModel.Protocols.OpenIdConnect.dll Assets/Auth0Lab/Plugins/Microsoft.IdentityModel.Tokens.dll Assets/Auth0Lab/Plugins/Microsoft.IdentityModel.JsonWebTokens.dll
Here is an example error for the incompatibility:
Assembly 'Assets/Auth0Lab/Plugins/System.IdentityModel.Tokens.Jwt.dll' will not be loaded due to errors: System.IdentityModel.Tokens.Jwt references strong named Newtonsoft.Json Assembly references: 10.0.0.0 Found in project: 12.0.0.0. Assembly Version Validation can be disabled in Player Settings "Assembly Version Validation"
If I disable Assembly Version Validation, Device Flow does not work anymore, as you can see in the comment above.
Would it be possible to update your version to 12.0.0.0? It would also make your solution compatible with Unity version 2020.1+
I got this error when building to Android using IL2CPP. The weird thing is that it works perfectly in the editor, on Android Mono build and Windows build. After troubleshooting for longer than I want to admit, I found that the error seems to be with code stripping.
By creating a file named link.xml, or reusing if you already have it in your Assets folder (see reference [2]), you can disable code stripping for the Auth0.AuthenticationApi dll by adding the following to the link.xml file:
<assembly fullname="Auth0.AuthenticationApi" preserve="all"/>
However, this only led me to the next error. I disabled code stripping on the rest of the dll's in the Assets/Auth0/Plugins folder by adding them to the link.xml file, so my link.xml file looks like this:
<linker>
<assembly fullname="Auth0.AuthenticationApi" preserve="all"/>
<assembly fullname="Auth0.Core" preserve="all"/>
<assembly fullname="Microsoft.IdentityModel.JsonWebTokens" preserve="all"/>
<assembly fullname="Microsoft.IdentityModel.Logging" preserve="all"/>
<assembly fullname="Microsoft.IdentityModel.Protocols" preserve="all"/>
<assembly fullname="Microsoft.IdentityModel.Protocols.OpenIdConnect" preserve="all"/>
<assembly fullname="Microsoft.IdentityModel.Tokens" preserve="all"/>
<assembly fullname="Newtonsoft.Json" preserve="all"/>
<assembly fullname="System.IdentityModel.Tokens.Jwt" preserve="all"/>
<assembly fullname="System.Security.Cryptography.Cng" preserve="all"/>
</linker>
DISCLAMER: I have not researched thoroughly enough how disabling code stripping affects the rest of the application, and I have not been able to take the time to look into which dll in particular is causing the issue.
References: [1] https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/Fix-AOT-using-link.xml [2] https://docs.unity3d.com/Manual/ManagedCodeStripping.html
@evenfl I can't thank you enough. Your solution solved my problem as well.
Can this fix be merged in? This package is super useful and people are still trying to use this