尝试设置自定义字段值时 Asana 错误请求 (API)

Asana bad request when attempting to set custom field values (API)

我正在尝试使用带有 https://app.asana.com/api/1.0/tasks 端点的 Asana API 创建一个新任务。我已经成功地在所需项目中创建了一些测试任务。但是,我现在也试图设置自定义字段的值,但似乎遇到了障碍。

我正在使用 Python 请求库。我感觉这是我格式化 post 的方式的问题。我已经检查了许多资源,包括这里,以及修补和修改我构造 "options" 字典的方式。然而,它似乎没有帮助。

def postTaskToAsana(taskName, taskURL, taskCreated, taskCompleted, taskDue):

token = "<TOKEN>"
bearerToken = "Bearer " + token
header = {
    "Authorization" : bearerToken
    }

options = {
    "projects" : ["234234234"],
    "name" : "Hello, World!",
    "notes" : "How are you",
    "assignee" : "2342342342",
    "custom_fields" : { "234234234234" : "hello" }
    }

url = "https://app.asana.com/api/1.0/tasks"
r = requests.post(url, headers=header, data=options)
return r

如果我从上面的选项字典中删除 "custom_fields",则 post 请求有效,我可以看到新创建的任务。我从上面的代码得到的响应是:

{"errors":[{"message":"Oops! An unexpected error occurred while processing this request. The input may have contained something the server did not know how to handle. For more help, please contact api-support@asana.com and include the error phrase from this response.","phrase":"9 "}]}

this post 中所述,目标是以这种格式为请求生成 JSON:

{
"data" : {
    "custom_fields" : { "2342342342" : "INFO" }
    }
}

据我所知,我的代码应该做什么。

如有任何帮助,将不胜感激。

[已解决]

我怀疑这个问题与我格式化请求的方式有关。由于我不清楚嵌套 custom_fields 字典创建的附加级别没有以正确的 JSON 格式格式化。我能够使用请求 JSON 参数解决此问题。

def postTaskToAsana(taskName, taskURL, taskCreated, taskCompleted, taskDue):

token = "<TOKEN>"
bearerToken = "Bearer " + token
header = {
    "Authorization" : bearerToken
    }

options = {
    "data" : {
        "projects" : ["123412341234"],
        "name" : "Review Task: " + taskName,
        "notes" : "Please review this task for where the process failed.\nTask: " + taskURL,
        "assignee" : "123412341234",
        "followers" : ["123412341234"],
        "custom_fields" : {
            "123412341234" : taskCreated,
            "123412342134" : taskDue,
            "123412341234" : taskCompleted
        }
    }
}

url = "https://app.asana.com/api/1.0/tasks"
r = requests.post(url, headers=header, json=options)
return r