简体   繁体   English

C#Asana POST新任务返回错误400(错误请求)

[英]C# Asana POST new task returns error 400 (Bad request)

I have a problem with creating new task in Asana from my app. 从我的应用程序在Asana中创建新任务时遇到问题。

Post method: 发布方法:

protected static T Post<T>(string route, object action = null, object parameters = null) where T : BaseResult, new()
    {
        var result = new T();

        try
        {
            var actionUrl             = GetActionUrl(route, action);
            var request               = (HttpWebRequest)WebRequest.Create(actionUrl);
            request.Credentials       = CredentialCache.DefaultCredentials;
            request.Accept            = "application/json";
            request.Method            = WebRequestMethods.Http.Post;
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            request.Headers.Add("Authorization: Bearer " + ApiKey);

            if (parameters != null)
            {
                var contentJSON       = JsonConvert.SerializeObject(parameters);
                request.ContentType   = "application/json";

                using (var s = request.GetRequestStream())
                    using (var sw = new StreamWriter(s, Encoding.UTF8))
                        sw.Write(contentJSON);
            }

            var response = (HttpWebResponse) request.GetResponse();

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                var data = reader.ReadToEnd();
                result   = JsonConvert.DeserializeObject<T>(data);
            }
        }
        catch (Exception ex)
        {
            result.IsOk    = false;
            result.Message = ex.GetMessage();
        }

        return result;
    }

Action URL: https://app.asana.com/api/1.0/workspaces/ MyWorkspace /tasks JSON: {"data":{"name":"TestTask1","notes":"Test note","workspace":"*MyWorkspace*","assignee":"*MyAssignee"}} 操作网址: https: //app.asana.com/api/1.0/workspaces/ MyWorkspace / tasks JSON: {"data":{"name":"TestTask1","notes":"Test note","workspace":"*MyWorkspace*","assignee":"*MyAssignee"}}

But Asana returns me "The remote server returned an error: (400) Bad Request." 但是Asana向我返回“远程服务器返回错误:(400)错误的请求。”

If I change request.ContentType to "application/x-www-form-urlencoded", I get no errors, but Asana returns me new task with empty fields. 如果将request.ContentType更改为“ application / x-www-form-urlencoded”,则不会出现任何错误,但是Asana会向我返回带有空字段的新任务。

What my next steps to fix issue should be? 我接下来要解决的问题应该是什么?

Thank you 谢谢

If you're using an ApiKey (and not a Personal Access Token), I believe that your Authorization Header should be 如果您使用的是ApiKey(而不是个人访问令牌),我认为您的授权标头应为

"Authorization: Basic " + EncodedAuthInfo

where 哪里

EncodedAuthInfo = Convert.ToBase64String(Encoding.Default.GetBytes(ApiKey + ":"))

See How do I connect to the Asana Rest API using c#? 请参阅如何使用c#连接到Asana Rest API? or the Using Basic Authentication section in https://asana.com/developers/documentation/getting-started/auth for details on using basic authentication. https://asana.com/developers/documentation/getting-started/auth中的“使用基本身份验证”部分,以获取有关使用基本身份验证的详细信息。

I'm also a little confused by what you mean when you say that 我对您所说的意思也感到困惑

JSON = {"data": {"name": "TestTask1"} ...

Is this the HTTP response that you expect? 这是您期望的HTTP响应吗?

Anyways, hopefully what I've outlined helps. 无论如何,希望我概述的内容对您有所帮助。

Hmm. I think I've got what's blocking you sorted out. 我想我已经解决了阻碍您解决的问题。

Imagine the scenario where you post to 想象一下您发布到的场景

https://app.asana.com/api/1.0/workspaces/123456/tasks

and you pass in the request body the parameter 然后将参数传递给请求主体

"workspace":"789012"

What should the Asana platform do with this data? Asana平台应如何处理这些数据? You've inadvertently specified the workspace twice with conflicting numbers. 您无意中两次指定了带有冲突数字的工作空间。 For this reason, you cannot specify the workspace id in the data when hitting an endpoint which also contains the workspace id in the URL . 因此, 在命中URL中也包含工作空间ID的端点时您无法在数据中指定工作空间ID

The documentation is confusing on this point, because we don't clarify which parameters are found in the URL and which parameters are found in the JSON in the request body. 关于这一点,文档令人困惑,因为我们没有澄清在请求正文中的URL中找到哪些参数以及在JSON中找到哪些参数。 I'm actually fixing this very soon! 我实际上很快就解决了! If this is indeed what's causing the issue, I'm sorry that we were not clear on this. 如果确实是引起此问题的原因,很抱歉我们对此不清楚。

Personally, I think it might be a better user experience to allow the workspace to be duplicated in the parameter data if and only if it's identical to the one in the URL, but right now, we simply check to see that there is only one value for the workspace id. 我个人认为,当且仅当工作空间与URL中的工作空间相同时,允许在参数数据中复制工作空间可能是更好的用户体验,但是现在,我们只是检查一下是否只有一个值为工作区ID。 If there are more than one, even if they are the same one, we return the 400 error code. 如果有多个,即使它们是相同的,我们也会返回400错误代码。

You might consider parsing the response body, even on errors. 您甚至可以考虑分析响应正文,即使有错误也可以。 In it, we try to provide fairly decent information about what was wrong about the request. 在其中,我们尝试提供有关请求错误之处的相当不错的信息。 For example, when testing my hunch about your request, what I got back was: 例如,当测试我对您的请求的预感时,我得到的是:

"errors":[{"message":"Duplicate field: workspace", ...}]

If we've done a good job about sending back informative messages, I hope you'll find this even more useful than an Asana sandbox! 如果我们在发送回信息方面做得很好,希望您会发现它比Asana沙箱更有用! If this is not the issue, feel free to comment; 如果这不是问题,请随时发表评论; I'll be happy to dive into this further. 我很乐意进一步探讨这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM