我一直在思考如何将 python 中的 Web 身份验证请求统一起来,有人可以帮我解决这个问题吗。
此处“auth”关键字的含义,以及我如何形成相同的形式 unity C#。 我是统一网络概念的新手,请帮助我。
This is my python code:
r = requests.post("my url",
auth = (client_id,client_secret),
data = { 'grant_type' : 'password',
'username' : username,
'password' : password })
if r. status_code == 200: { my code ...}
In unity i dont know how to write this, i read through WWW,WWWForm classes and used headers also like shown below but always getting 404 error. page not displayed.
This is my unity code :
publicvoidRequestUserLogin(stringpUsername, stringpPassword)
{
Dictionary<string,string> authHeader = newDictionary<string,string> ();
authHeader.Add (mS_Client_Id, mS_Client_Secret);
WWWForm form = newWWWForm();
WWW WWWLogin;
form.AddField ("grant_type", "password");
form.AddField ("username", "dumandu1");
form.AddField ("password", "pwd");
Hashtableheaders = form.headers;
headers ["auth"] = System.Convert.ToBase64String
(System.Text.Encoding.ASCII.GetBytes(mS_Client_Id+":"+mS_Client_Secret));
stringurl = "my url";
WWWLogin = newWWW(url,form);
StartCoroutine(Example(WWWLogin));
}
IEnumeratorExample(WWWWWWLogin)
{
yield return WWWLogin;
if (WWWLogin.error == null)
{
Debug.Log("WWW Login Success: "+ WWWLogin.error);
if (WWWLogin.text != null)
{
Debug.Log("WWW Login Text: "+ WWWLogin.text);
}
}
else
{
Debug.Log("WWW Login Error: "+ WWWLogin.error);
}
}
为此,我总是收到 404 页面未显示错误。代码在 python 中运行良好,并从服务器获得正确的响应。
我是不是做错了什么。
any help would be great .
thank you.
最佳答案
始终确保在调用任何函数时使用所有必要的数据。
在这种情况下,您将忽略 header 。 new WWW(url, form)
将创建一个没有 header 的请求。你想要的是:
WWWLogin = new WWW(url, form, headers);
希望对您有所帮助。
关于c# - 将 python 服务器请求转换为统一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29003099/