通过tenderapp api创建新用户

时间:2011-03-21 16:11:21

标签: .net-3.5

想知道你是否可以提供帮助。我在.net 3.5框架上使用c#。我试图通过tenderapp api创建用户的地方。在每次尝试时,我得到一个'远程服务器返回错误:(422)不可处理的实体。 ”。

我正在使用以下代码:

string username = "Eddie";
string password = "password";
string tenderUrl = string.Format("https://{0}:{1}@api.tenderapp.com/sitekey/users", username, password);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tenderUrl);
request.Accept = "application/vnd.tender-v1+json";
request.ContentType = "application/json";
request.Method = "POST";
string postData =  "{'email':'***@eddie.com','password':'test','password_confirmation':'test'}";


byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

WebResponse response = request.GetResponse();//error returned here

Console.WriteLine(((HttpWebResponse)response).StatusDescription);

dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);

reader.Close();
dataStream.Close();
response.Close();

不确定postData字符串是否导致错误。有关详细信息,请参阅https://help.tenderapp.com/kb/api/users

如果有人可以指出我正确的方向,那将会很棒。

谢谢, 埃迪

1 个答案:

答案 0 :(得分:0)

有这个工作。创建新用户的密码必须至少为字符。整理了一些东西,但这是工作代码。

public void createTenderUser(string supportUser, string supportPassword, string userEmail, string userPassword)
{

    //Throw argument exceptions 

    string tenderUrl = string.Format("https://{0}:{1}@api.tenderapp.com/iremoco/users/", HttpUtility.UrlEncode(supportUser), HttpUtility.UrlEncode(supportPassword));
    string postData = string.Format("{{'email':'{0}','password':'{1}','password_confirmation':'{1}'}}", userEmail, userPassword);
    if (userPassword.Length >= 6)
    {
        try
        {
            System.Net.ServicePointManager.Expect100Continue = false;
            var request = (HttpWebRequest)WebRequest.Create(tenderUrl);
            request.Accept = "application/vnd.tender-v1+json";
            request.ContentType = "application/json";
            request.Method = "POST";
            byte[] bytes = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = bytes.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(bytes, 0, bytes.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
        }
        catch
        {
            //display error
        }
    }
    else { 
        //display error
    }
}

谢谢, 埃迪