Issue with Auth when running outside of Unity editor
-
I've recent been testing a game within the Unity editor without issue, so decided to try and run it on device (Mac, iPhone and Android), however at this point anonymous auth seems to stop working with this error:
Received an invalid json format response, check your network settings. GameManager:onAuthFailure(Int32, Int32, String, Object) BrainCloudWrapper:AuthFailureCallback(Int32, Int32, String, Object)
Code:
public void SetupPlayer(bool tryGameCentre = false) { if (!Bc.Client.IsAuthenticated()) { Bc.AuthenticateAnonymous(new SuccessCallback(onAuthSuccess), new FailureCallback(onAuthFailure)); } }
public void onAuthFailure(int status, int errorCode, string jsonError, object sender) { Debug.Log($"{jsonError}"); }
public void onAuthSuccess(string jsonResponse, object sender) { var response = JObject.Parse(jsonResponse); if (response == null) { Debug.Log("Error logging in"); return; } currentUser = UserModel.FromJson(response["data"].ToString()); #if UNITY_IOS Debug.Log("IOS RUNNING"); StartCoroutine(RequestAuthorization()); #endif GetGameList(); UserAuthenticationSuccessful(currentUser.SessionId != null ? true : false); }
this all works fine in the editor, it is only on device
-
Hello Gavin,
I'm having trouble reproducing your issue to better give you a solution. What version of Unity Editor are you using? Are you properly initializing on the device before requesting authentication? I have a feeling this might be a permissions issue for network access based on the error message you received. Any other details would be helpful so that I can reproduce this error to find a solution for you. -
Hi Franco,
I am not sure the issue is anything to do with networking, it fully works from Unity editor and I think the error from brainCloud is potentially misleading.
I am using Unity 2021.3.8f1
After tracing it back tracing it back through, the error is being generated byBrainCloudComms.cs, specifically private void HandleResponseBundle(string jsonData), If I enable debugging and run on an iPhone, I get output of:
19:22:40.664 #BCC RESPONSE - 21/04/2023 19:22:40 {"packetId":0,"responses":[{"data":{"abTestingId":30,"lastLogin":1682102815319,"server_time":1682102815403,"refundCount":0,"timeZoneOffset":0.0,"experiencePoints":200,"maxBundleMsgs":10,"createdAt":1682102815302,"parentProfileId":null,"emailAddress":null,"experienceLevel":1,"countryCode":"GB","vcClaimed":0,"currency":{"coins":{"consumed":0,"balance":200,"purchased":0,"awarded":200}},"id":"b49b86a2-6b23-4467-8a8b-ab7208fa40f7","compressIfLarger":51200,"amountSpent":0,"previousLogin":null,"playerName":"player234147492","pictureUrl":null,"incoming_events":[],"sessionId":"e6r6fmaatt4u32891m6en7htct","languageCode":"en","vcPurchased":0,"isTester":false,"summaryFriendData":null,"loginCount":1,"emailVerified":true,"xpCapped":false,"profileId":"b49b86a2-6b23-4467-8a8b-ab7208fa40f7","newUser":"true","playerSessionExpiry":1200,"sent_events":[],"maxKillCount":11,"rewards":{"experiencePoints":200,"rewardDetails":{"xp":{"experienceLevels":[{"level":1,"rewards":{"experiencePoints":200,"currency":{"coins":200}}}],"experiencePoints":200}},"xpCapped":false,"currency":{"coins":{"consumed":0,"balance":200,"purchased":0,"awarded":200}},"experienceLevel":1,"rewards":{"experienceLevels":[1],"experiencePoints":200,"currency":{"coins":200}}},"statistics":{}},"status":200}]} false
The function then tries to decode the json:
JsonResponseBundleV2 bundleObj = DeserializeJsonBundle(jsonData);
The next bit of output is then:
19:22:40.666 #BCC Only objects with default constructors can be deserialized. (BrainCloud.Internal.JsonResponseBundleV2) UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object) UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[]) UnityEngine.Logger:Log(LogType, Object) UnityEngine.Debug:Log(Object) BrainCloud.BrainCloudClient:Log(String) BrainCloud.Internal.BrainCloudComms:DeserializeJsonBundle(String) BrainCloud.Internal.BrainCloudComms:HandleResponseBundle(String) BrainCloud.Internal.BrainCloudComms:Update() BrainCloud.BrainCloudClient:Update(eBrainCloudUpdateType) BrainCloudWrapper:RunCallbacks() BrainCloudWrapper:Update() Received an invalid json format response, check your network settings.
So data is definitely making it back from the server, but then if I log in from Unity editor, the response from BrainCloud looks similar?
{"packetId":0,"responses":[{"data":{"abTestingId":6,"lastLogin":1682102456093,"server_time":1682102456127,"refundCount":0,"timeZoneOffset":0.0,"experiencePoints":200,"maxBundleMsgs":10,"createdAt":1682102419336,"parentProfileId":null,"emailAddress":null,"experienceLevel":1,"countryCode":null,"vcClaimed":0,"currency":{"coins":{"consumed":0,"balance":200,"purchased":0,"awarded":200}},"id":"cd068c0b-d7f6-41de-85bd-8830631020e3","compressIfLarger":51200,"amountSpent":0,"previousLogin":1682102419345,"playerName":"kohatu918618012","pictureUrl":null,"incoming_events":[],"sessionId":"i405rjp0s9b3unisrfedpsem0b","languageCode":"en","vcPurchased":0,"isTester":false,"summaryFriendData":null,"loginCount":2,"emailVerified":true,"xpCapped":false,"profileId":"cd068c0b-d7f6-41de-85bd-8830631020e3","newUser":"false","playerSessionExpiry":1200,"sent_events":[],"maxKillCount":11,"rewards":{"rewardDetails":{},"currency":{},"rewards":{}},"statistics":{}},"status":200}]}
-
Hello Gavin,
Unfortunately I still cannot reproduce the error you're reporting and I've tested against all the devices you've mentioned previously. I would need more information of what your process is in your project that gives you this error that could be helpful for me to reproduce the error. Please provide any and all information or contact us at our intercom on-line chat support to provide an example project or link to project for me to work with. In the meantime I would suggest for you to check out our Authentication Example in our examples-unity repository in Github, perhaps it will provide a solution for you.
Unity Examples: https://github.com/getbraincloud/examples-unity -
Hello @Gavin-Beard!
We've had trouble reproducing this issue on our end. After some testing with a small app on MacOS and iOS, we have not been able to see the error you are encountering using Unity 2021.3.8.
If you could, please check out the app here and let us know if this issue is still persisting. It is a barebones authentication app and it should run on multiple devices. The
Authentication.cs
script has all the gory details. Just open up the Main scene underAssets > App > Misc > Main.unity
to get it running. You will also need to add your brainCloud credentials, but you can copy theAuthentication
example template in brainCloud to test the app.Please let us know how this app works on your end and if any issues are encountered! You should be able to click on any error logs in the in-app console to copy the log to your device's clipboard.
As for your specific issue, my best guess is that perhaps an external library of sorts might not be supported with brainCloud, such as the Newtonsoft Json.NET library. For JSON de/serializing, brainCloud comes with JsonFX. You can also make use of Unity's own JsonUtility for structured JSON de/serialization. Both the app I've shared and the examples on our GitHub make use of JsonFX extensively.
If you'd like to know more and see how a more robust app can handle brainCloud authentication, as Franco suggested, you should check out our Authentication example on our Unity Examples GitHub.
Hope this helps! Please let us know if you have further questions or inquiries about this.