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

时间:2015-03-12 06:21:56

标签: c# python web unity3d

我正在试图将在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.

2 个答案:

答案 0 :(得分:0)

在调用任何函数时,请务必确保使用所有必要的数据。

在这种情况下,您要退出标题。 new WWW(url, form)将创建一个没有标头的请求。你想要的是:

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

希望有所帮助。

答案 1 :(得分:0)

修改后的帖子(答案):

public void RequestUserLogin(string pUsername,string pPassword)     {         WWWForm form = 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);
    }  
}