【问题标题】:Trello API / Python - Can't Set Custom Field Value For CardTrello API / Python - 无法为卡片设置自定义字段值
【发布时间】:2020-10-18 12:45:18
【问题描述】:

我正在使用 python 来编辑我的 Trello 卡片 - 具体来说,我正在尝试更新卡片上的自定义字段值。

我的 python 脚本可以将字段的值设置为 "" 就好了,但是当我尝试将值设置为非空字符串时,它给出了一个错误:

'自定义字段项值无效。', 'error': 'ERROR'

我认为这可能与编码有关,但是在尝试将值 dict 编码为 json 并将请求标头设置为“application/json”之后,两者都无济于事,我没有想法。

谁能帮我解决这个问题?

代码:

def trello_card_update_custom_field(card_id, custom_field_id, custom_field_value):
    url = "https://api.trello.com/1/cards/{card_id}/customField/{custom_field_id}/item".format(card_id=card_id, custom_field_id=custom_field_id)
    querystring = {
        "key": key,
        "token": token,
        "value": {'text': 'this is a new string'}
        #"value": ""
    }
    response = requests.request("PUT", url, params=querystring)
    response_json = response.json()
    return response_json
# end function


update_trello_card = trello_card_update_custom_field(card_id, custom_field_id, custom_field_value)
print("updated trello card. json response:\n", update_trello_card)

这是文档中此函数的链接:https://developer.atlassian.com/cloud/trello/rest/#api-cards-idCard-customField-idCustomField-item-put

edit - 我尝试更改请求的嵌套 dict 部分:

"value": {'text': 'this is a new string'}

只是:

'text': 'this is a new string'

但是我收到了这个错误:

Traceback (most recent call last):
  File "test_trello.py", line 153, in <module>
    update_trello_card = trello_card_update_custom_field_path_auth (card_id, custom_field_id, custom_field_value)
  File "test_trello.py", line 129, in trello_card_update_custom_field_path_auth
    response_json = response.json()
  File "C:\Users\andre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我还尝试将请求编辑为:

'value': '这是一个新字符串'

但是我收到了这个错误:

updated trello card custom field. json response:
 {'message': 'Invalid custom field item value.', 'error': 'ERROR'}

我也尝试将字段值作为“data”kwarg 发送,而不是在“params”kwarg 中发送,但没有任何区别。

【问题讨论】:

标签: python-3.x api trello


【解决方案1】:
def trello_card_update_custom_field(card_id, custom_field_id, custom_field_value):
url = "https://api.trello.com/1/cards/{card_id}/customField/{custom_field_id}/item".format(card_id=card_id, custom_field_id=custom_field_id)
querystring = {
    "key": key,
    "token": token,
}
body = {'text': 'this is a new string'}  
response = requests.request("PUT", url, data=body, params=querystring)
response_json = response.json()
return response_json

【讨论】:

  • 是的,这与我使用的代码相同,但它仍然给出与上面相同的错误(取决于我将值称为“值”还是“文本”)
  • 如同,这与我尝试的代码变体之一相同。我已经尝试了几十个小变化,以及将请求发送为“json=body”,以及“data=json.dumps(body)”和“json=json.dumps(body)”。似乎没有任何效果
  • 根据您的链接,无论是否带有“headers”参数,它也没有什么区别。
  • 我认为我的下一步是尝试用完全不同的语言执行此操作,看看它是否仍然会出错。在这种情况下,这意味着它是某种编码问题?
  • 也许您应该添加标头以请求将正文声明为 json。
【解决方案2】:
body=
{
  "value": {
    "text": "<string>",
    "checked": true,
    "date": "2018-03-13T16:00:00.000Z",
    "number": 2154
  }
}

试试这个身体

这里也指: https://developer.atlassian.com/cloud/trello/rest/#api-cards-idCard-customField-idCustomField-item-put

并单击正文参数示例

【讨论】:

  • 回溯(最近一次调用最后):文件“test_trello.py”,第 158 行,在 update_trello_card = trello_card_update_custom_field_path_auth (card_id, custom_field_id, custom_field_value) 文件“test_trello.py”,第 123 行,在 trello_card_update_custom_field_path_auth "checked": true, NameError: name 'true' is not defined
  • 自定义字段是一个文本字段,因此它会拒绝复选框值是有道理的。并且 trello 没有更新
【解决方案3】:

我遇到了同样的问题,很高兴为您提供一个可行的解决方案(我第一次在这个网站上回答?)。

您的回复定义:response = requests.request("PUT", url, params=querystring)

您需要像这样修改它: response = requests.request("PUT", url, json=querystring)

这是我的功能:

def update_custom_field(card_id,custom_field_id,value_type,value):
   url = f'https://api.trello.com/1/card/{card_id}/customField/{custom_field_id}/item'
   payload = {'token':token,'key':key,'value':{value_type: value}}
   request = requests.put(url,json=payload) 
   return request.text

update_custom_field(card_id,custom_field_id,'text','hello word!')```

【讨论】:

  • 像魅力一样工作。我还建议将tokenkey 添加为参数,以便可以使用environ.get() 检索它们。
猜你喜欢
  • 2018-09-02
  • 1970-01-01
  • 2017-01-07
  • 2021-09-12
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多