Hello thanks a lot for the video! In my case we used the same approach we have multiple API services and the unity app is talking with them. I saw that in your project you used a simple singleton for the object that is calling the API. Do you know if there is a better way to implement the service on the frontend? Because we used singletons to and now its really impossible to test without the DB. Thanks!!
At 33:55 in the video, when I press register I get an error message {"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."]}, anyone have any idea what I could be doing wrong and how to fix this?
string jsonData = JsonUtility.ToJson(register); this code removes Email and only save password and confirm password into byte array, that is the reason you are getting the error.
Hey I know it's an old video but do you have any idea why JsonUtility.ToJson doesn't work with the shorthand getters and setters. If I delete them and only declare the properties the class should have, without getters and setters, then JsonUtility.ToJson works fine.
When saving the data I get this message: "Message":"Authorization has been denied for this request. form.headers.Add("Authorization", "Bearer "+ login.access_token); UnityWebRequest webRequest = UnityWebRequest.Post(baseURL + "api/userprofile", form); How to solve?
I had this same issue, and it turned out to be the way the UnityWebRequest Register() method was sending data with the json utility. To fix this I changed it to also be a WWWForm as follows: public IEnumerator Register(Register register) { WWWForm form = new WWWForm(); form.AddField("Email", emailInputField.text); form.AddField("username", emailInputField.text); form.AddField("password", passwordInputField.text); form.AddField("password_confirmation", confirmPasswordInputField.text); UnityWebRequest uwr = UnityWebRequest.Post(baseUrl + "/api/Account/Register", form);
yield return uwr.SendWebRequest(); if (uwr.result == UnityWebRequest.Result.ConnectionError) { Debug.Log("Error" + uwr.error); } else { Debug.Log(uwr.downloadHandler.text); } I will also post my LogIn() and SaveData() methods to help incase you end up needing them too. A couple things to note, in the LogIn method it didnt make much sense to call the SaveData() coroutine since in general people want to load character data while logging in so, thus I added that to mine. Also make sure to either add or remove the "api/" from your base url depending on how you particularly implement the code or what else youre adding to the string. public IEnumerator LogIn() { WWWForm form = new WWWForm(); form.AddField("grant_type", "password"); form.AddField("Email", loginEmailInputField.text); form.AddField("username", loginEmailInputField.text); form.AddField("password", loginPasswordInputField.text); UnityWebRequest uwr = UnityWebRequest.Post( baseUrl +"token", form);
yield return uwr.SendWebRequest(); if (uwr.result == UnityWebRequest.Result.ConnectionError ) { Debug.Log("Error" + uwr.error); } else { Debug.Log(uwr.downloadHandler.text); logIn = JsonUtility.FromJson(uwr.downloadHandler.text); //Debug.Log(logIn.access_token.ToString()); //StartCoroutine(SaveData()); //StartCoroutine(LoadData()); } } Finally here is the SaveData() method: public IEnumerator SaveData() { WWWForm form = new WWWForm(); form.AddField("Email", email); form.AddField("username", email); form.AddField("UserData", "some string of information such as item id or whatever you want"); //Debug.Log(logIn.access_token);
Question, you've used the column 'UserData' but where exactly are you filling the information from that field when creating the user? Because mine is empty, where as other columns as username and password are filled in correctly.
I found the solution. Still not sure why the method shown was not working, but in the SaveData() method, it is no longer: form.AddField("username", loginEmailInputField.text); form.AddField("UserData", "{score:15}"); form.headers.Add("Authorization", "Bearer " + logIn.access_token); UnityWebRequest uwr = UnityWebRequest.Post(baseURL + "API/UserProfile", form); Instead, it should be as follows: form.AddField("username", loginEmailInputField.text); form.AddField("UserData", "{score:15}"); UnityWebRequest uwr = UnityWebRequest.Post(baseURL + "API/UserProfile", form); uwr.SetRequestHeader("Authorization", "Bearer " + logIn.access_token); Hopefully this can save some poor soul the hours it took me to brute force this solution haha.
@@ethanohmer hi mate, did you find a solution to the {"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."]} error?
@@harrybuscema1582 make sure to add: form.AddField("Email", email); form.AddField("username", email); to both the Register(), SaveData(), and LogIn() methods. I had to end up using WWWForm for all three to properly send data. Also, when logging in, the username must be an email.
I just added my full methods to the user "Zohan's world" comment down below which may also be useful to you. If you're still unable to get it working I would be open to helping further over discord or something so you can stream and work through the issue.
I always get this debug when trying to register. Is there an slightly updated version or something simply wrong? {"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."],"model.Password":["The Password field is required."]}} UnityEngine.Debug:Log (object) NetworkingManager/d__16:MoveNext () (at Assets/Scripts/NetworkingManager.cs:65) UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
🎮 Unity Masterclass - Master game development by building enjoyable games from scratch: bit.ly/3YJWe0N
Hello thanks a lot for the video! In my case we used the same approach we have multiple API services and the unity app is talking with them. I saw that in your project you used a simple singleton for the object that is calling the API. Do you know if there is a better way to implement the service on the frontend? Because we used singletons to and now its really impossible to test without the DB. Thanks!!
In you user registration you may have dublicates in your database
Supremely helpful tutorial/ Thanks.
At 33:55 in the video, when I press register I get an error message {"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."]}, anyone have any idea what I could be doing wrong and how to fix this?
hey! have you resolved the issue?
I have the same one and don't know what to do (:
Did anyone ever find the solution to this?
@@pan1k524 nope couldn't work it out
string jsonData = JsonUtility.ToJson(register); this code removes Email and only save password and confirm password into byte array, that is the reason you are getting the error.
Hey I know it's an old video but do you have any idea why JsonUtility.ToJson doesn't work with the shorthand getters and setters. If I delete them and only declare the properties the class should have, without getters and setters, then JsonUtility.ToJson works fine.
When saving the data I get this message:
"Message":"Authorization has been denied for this request.
form.headers.Add("Authorization", "Bearer "+ login.access_token);
UnityWebRequest webRequest = UnityWebRequest.Post(baseURL + "api/userprofile", form);
How to solve?
I had this same issue, and it turned out to be the way the UnityWebRequest Register() method was sending data with the json utility. To fix this I changed it to also be a WWWForm as follows:
public IEnumerator Register(Register register)
{
WWWForm form = new WWWForm();
form.AddField("Email", emailInputField.text);
form.AddField("username", emailInputField.text);
form.AddField("password", passwordInputField.text);
form.AddField("password_confirmation", confirmPasswordInputField.text);
UnityWebRequest uwr = UnityWebRequest.Post(baseUrl + "/api/Account/Register", form);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("Error" + uwr.error);
}
else
{
Debug.Log(uwr.downloadHandler.text);
}
I will also post my LogIn() and SaveData() methods to help incase you end up needing them too. A couple things to note, in the LogIn method it didnt make much sense to call the SaveData() coroutine since in general people want to load character data while logging in so, thus I added that to mine. Also make sure to either add or remove the "api/" from your base url depending on how you particularly implement the code or what else youre adding to the string.
public IEnumerator LogIn()
{
WWWForm form = new WWWForm();
form.AddField("grant_type", "password");
form.AddField("Email", loginEmailInputField.text);
form.AddField("username", loginEmailInputField.text);
form.AddField("password", loginPasswordInputField.text);
UnityWebRequest uwr = UnityWebRequest.Post( baseUrl +"token", form);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError )
{
Debug.Log("Error" + uwr.error);
}
else
{
Debug.Log(uwr.downloadHandler.text);
logIn = JsonUtility.FromJson(uwr.downloadHandler.text);
//Debug.Log(logIn.access_token.ToString());
//StartCoroutine(SaveData());
//StartCoroutine(LoadData());
}
}
Finally here is the SaveData() method:
public IEnumerator SaveData()
{
WWWForm form = new WWWForm();
form.AddField("Email", email);
form.AddField("username", email);
form.AddField("UserData", "some string of information such as item id or whatever you want");
//Debug.Log(logIn.access_token);
UnityWebRequest uwr = UnityWebRequest.Post(baseUrl = "/API/UserProfile", form);
uwr.SetRequestHeader("Authorization", "Bearer " + logIn.access_token);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("Error" + uwr.error);
}
else
{
Debug.Log(uwr.downloadHandler.text);
}
}
@@ethanohmer Thanks for the solution :)
AWESOME MAN! Is there a way where the user only gets logged in successfully if the owner of the firebase (in the case, me) accepts the login?
Question, you've used the column 'UserData' but where exactly are you filling the information from that field when creating the user? Because mine is empty, where as other columns as username and password are filled in correctly.
The Get and Post method written in UserProfileController donot work. Wrong
Hi, Im trying to create azure app service, what should i do as runtime stack? .
How to integrate character select?
I keep getting an "Authorization has been denied for this request." after attempting to implement the SaveData(). Any ideas?
I found the solution. Still not sure why the method shown was not working, but in the SaveData() method, it is no longer:
form.AddField("username", loginEmailInputField.text);
form.AddField("UserData", "{score:15}");
form.headers.Add("Authorization", "Bearer " + logIn.access_token);
UnityWebRequest uwr = UnityWebRequest.Post(baseURL + "API/UserProfile", form);
Instead, it should be as follows:
form.AddField("username", loginEmailInputField.text);
form.AddField("UserData", "{score:15}");
UnityWebRequest uwr = UnityWebRequest.Post(baseURL + "API/UserProfile", form);
uwr.SetRequestHeader("Authorization", "Bearer " + logIn.access_token);
Hopefully this can save some poor soul the hours it took me to brute force this solution haha.
@@ethanohmer hi mate, did you find a solution to the {"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."]} error?
@@harrybuscema1582 make sure to add:
form.AddField("Email", email);
form.AddField("username", email);
to both the Register(), SaveData(), and LogIn() methods. I had to end up using WWWForm for all three to properly send data. Also, when logging in, the username must be an email.
I just added my full methods to the user "Zohan's world" comment down below which may also be useful to you. If you're still unable to get it working I would be open to helping further over discord or something so you can stream and work through the issue.
@@ethanohmer yep thanks so much mate, life saver, worked perfectly!
token wont output for me
I always get this debug when trying to register. Is there an slightly updated version or something simply wrong?
{"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."],"model.Password":["The Password field is required."]}}
UnityEngine.Debug:Log (object)
NetworkingManager/d__16:MoveNext () (at Assets/Scripts/NetworkingManager.cs:65)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
14:39 just for myself
Do you have a Discord server?
Yes, we do! discord.com/invite/zwbrpCNB2M
How secure is this?