更新:位于底部
注意:
我已经做了大量的搜索,试图找到答案,但无济于事。
我正在尝试创建一个统一的脚本,该脚本登录到我的Laravel应用程序并返回身份验证令牌,以便我可以使用它来发出API请求。令牌正确返回。
C#代码:
private IEnumerator SendLogin(string user, string pass, Action<bool, string> callback)
{
// Build login Submission form.
WWWForm formData = new WWWForm();
formData.AddField("email", user);
formData.AddField("password", pass);
formData.AddField("_token", "W4d7JugbWRg4ZASe4qAPinJVgg35OeaYStgeE7dm");
// Checking Username and password were read correctly (they are)
Debug.Log("Username: " + user + " Password: " + pass);
// Create Request
WWW www = new WWW(loginServerUrl, formData);
Debug.Log("Sending Login Info To Server.");
// Wait for request to finish
yield return www;
// just incase?
while (!www.isDone)
{
yield return new WaitForSecondsRealtime(0.15f);
Debug.Log("Waiting for download to finish.");
}
// Check for errors
if (www.error != null)
{
foreach (KeyValuePair<string, string> header in www.responseHeaders)
{
Debug.Log(header.Key.ToString() + " Value: " + header.Value.ToString());
}
Debug.Log("Error On Request. To string:" + www.error.ToString());
StopCoroutine(login_coroutine);
}
// Get Response
string response = www.text;
Debug.Log(response);
// Parse JSON
var json = JSON.Parse(www.text);
Debug.Log("Json: " + json.ToString());
Debug.Log("Response: " + response);
if (callback != null)
{
callback(true, response);
}
StopCoroutine(login_coroutine);
}
Unity屏幕截图(包括控制台日志): (URL不同,我在/ login上获得相同的结果)
修改 我编辑了登录控制器以返回输入的电子邮件和密码作为回复,我得到了一些有趣的结果 我还检查了访问日志,并且有一个奇怪的相关性。 “\ u200b”被添加到用户名和密码的末尾。
日志: 192.168.178.80 - - [02 / Feb / 2018:11:03:47 +0000]“POST / login HTTP / 1.1”401 12“ - ”“UnityPlayer / 2018.1.0b4(UnityWebRequest / 1.0,libcurl / 7.51.0- DEV)“” - “ 192.168.178.80 - - [02 / Feb / 2018:12:55:33 +0000]“POST / login HTTP / 1.1”200 361“ - ”“Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36( KHTML,像Gecko)Chrome / 63.0.3239.132 Safari / 537.36“” - “
答案 0 :(得分:0)
Unity在用户名和密码的末尾添加了零宽度空间。 (用.TrimEnd('\ u200b')修剪它们)
WWWForm formData = new WWWForm();
formData.AddField("email", user.TrimEnd('\u200b'));
formData.AddField("password", pass.TrimEnd('\u200b'));
formData.AddField("_token", {{YOUR TOKEN}});