我写了一个程序,通过API向asana发布任务,直到今天早上一直工作正常,任何人都可以帮我找出原因吗?
这是我发送的JSON字符串的示例:
{"workspace":09876543321111,"data": {"assignee":null,"name":"Sample Name","notes":"Sample Noted","due_on":"2015-01-27","projects":"12434567889099","completed":false}}
我收到400错误:错误请求。
这是我的代码:
string ID = "09876543321111"; //workspace ID
string url = @"https://app.asana.com/api/1.0/workspaces/" + ID + @"/tasks";
Data dat = new Data();
string ProjName = "Test Project";
dat.projects = "1234567890234";
dat.assignee = null;
dat.name = "Sample Name";
dat.notes = "Sample Notes";
dat.due_on = val.requiredBy.Value.ToString("u").Substring(0, 10);
dat.completed = false;
//if task doesnt exist, make one
if (!Tasks.CheckExist(project, dat.projects, dat.name, apiKey, log))
{
string json = JsonConvert.SerializeObject(dat);
string data = "{\"workspace\":" + ID + ",\"data\": " + json + "}";
log.WriteLine(data);
Functions.Post(data, url, apiKey, log);
}
发布功能:
//post tasks to asana
public static void Post(string data, string url, string apiKey, StreamWriter log)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = WebRequestMethods.Http.Post;
req.ContentLength = bytes.Length;
req.ContentType = "application/json";
var authInfo = apiKey + ":";
var encodedAuthInfo = Convert.ToBase64String(
Encoding.Default.GetBytes(authInfo));
req.Headers.Add("Authorization: Basic " + encodedAuthInfo);
req.ContentLength = bytes.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Close();
try
{
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
catch (WebException ex)
{
HttpWebResponse response = ((HttpWebResponse)ex.Response);
string exc = url + " caused a " + (int)response.StatusCode + " error.\n" + response.StatusDescription;
Console.WriteLine(exc);
log.WriteLine(exc);
}
}
修改
对于任何关心我的人我通过将字符串数据更改为:
来解决问题 string data = "{\"data\": " + json + "}";
答案 0 :(得分:0)
如果在顶级传递了意外参数,我们最近做了一个更改以返回400错误,因为(几乎)所有API路由仅使用在"数据"下传递的参数。属性。在这种情况下(正确确定)"工作空间"顶级属性是不正确的 - 以前我们只是忽略它,但是为了减少API而且令人惊讶"我们希望对可以忽略的参数保持明确和严格,否则可能会产生误导。