WWW / WWWForm发布JSON数据C#(Unity)

时间:2018-09-16 19:12:31

标签: c# unity3d

首先,您对我的英语不好感到抱歉,我需要帮助来建立自己的roblox身份验证系统,我使用roblox auth api:https://auth.roblox.com/docs#!/Authentication/post_v2_login,但是我无法从服务器获得有效的响应。我收到错误代码400“需要用户名和密码。请重试。”

我测试了以下curl命令结构,它工作正常:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"ctype": "Username", \"cvalue": "username", \"password": "password" \}' 'https://auth.roblox.com/v2/login'

我的c#脚本:

IEnumerator request()
{
    var content = "{ \"ctype\" : \"Username\" , \"cvalue\": \"username\", \"password\" : \"password\"}";

    var encoding = new System.Text.UTF8Encoding();

    var headers = new Hashtable ();

    headers.Add("Content-Type", "application/json");
    headers.Add("Accept", "application/json");
    headers.Add ("X-CSRF-TOKEN", "sendfalsetokken");

    //Send a request with a bad tokken to get a valid token from roblox system
    var request = new WWW ("https://auth.roblox.com/v2/login", encoding.GetBytes (content), headers);

    yield return request;

    //I get a error 403 Token Validation Failed it's logic the token in headers is bad
    Debug.Log (request.text);

    //this foreach allow me to recover the token from roblox
    foreach(KeyValuePair<string, string> entry in request.responseHeaders)
    {
        if (entry.Key == "X-CSRF-TOKEN") {
            Tokken = entry.Value;
        }
    }

    //And finally I resend a new post request with a correct token
    headers = new Hashtable();

    headers.Add("Content-Type", "application/json");
    headers.Add("Accept", "application/json");
    headers.Add ("X-CSRF-TOKEN", Tokken);


    request = new WWW ("https://auth.roblox.com/v2/login", encoding.GetBytes (content), headers);

    yield return request;

    //And I get a error 400 Username and Password are required. Please try again. I don't know why ...
    Debug.Log (request.text);


    yield break;
}

感谢您的帮助。

0 个答案:

没有答案