将 python 服务器请求转换为统一

Converting a python server request into unity

我一直在思考如何将 python 中提出的 Web 身份验证请求统一起来,谁能帮我解决这个问题。

这里 'auth' 关键字是什么意思,我如何形成相同的形式统一 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.

始终确保在调用任何函数时使用所有必要的数据。

在这种情况下,您将 headers 排除在外。 new WWW(url, form) 将创建一个没有 headers 的请求。你想要的是:

WWWLogin = new WWW(url, form, headers);

希望对您有所帮助。

修改Post(答案):

public void RequestUserLogin(string pUsername, string pPassword) { WWWForm 表单 = new WWWForm();

    form.AddField ("username", pUsername);
    form.AddField ("password", pPassword);
    form.AddField ("grant_type", "password");

    Hashtable headers = form.headers;
    byte[] rawData = form.data;

    headers ["Authorization"] = "Basic "+ System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(mS_Client_Id+":"+mS_Client_Secret));
    string url = "My URL";

    WWW WWWLogin = new WWW(url,rawData,headers);
    StartCoroutine(Example(WWWLogin));
}

IEnumerator Example(WWW WWWLogin) 
{
    yield return WWWLogin;

    if (WWWLogin.error == null)
    {
        Debug.Log("WWW Login Success: "+WWWLogin.text);
    }  
    else 
    {
        Debug.Log("WWW Login Error: "+ WWWLogin.error);
    }  
}