如何将 trello 上传和图像到 Trello,但不将其设置为 Cover这是默认设置

问题描述

我正在使用 Trello API 将图像上传到 Trello 卡片。认情况下,Trello 将图像设置为封面图像。 API 文档说有一个 setCover 选项,但我无法让它工作。

https://developer.atlassian.com/cloud/trello/rest/api-group-cards/#api-cards-id-attachments-post

def __upload_image_to_card(self,connection: dict,card: dict,image_path : str,cover=False):
    params = (
        ('key',connection['trello_api_key']),('token',connection['trello_api_token']),)
    
    values = {
        'file': (image_path,open(image_path,'rb')),'setCover' : (cover),}

    # Todo: Move to node_manager
    response = requests.post('https://api.trello.com/1/cards/%s/attachments' % card['id'],params=params,files=values).json()

我试过在布尔值和字符串之间切换,也试过去掉括号。我什至在 values 中添加一个错误键,以查看是否可以获得不同的响应。没有阻止图像上传,没有阻止图像成为认图像。

非常感谢您的帮助。

解决方法

setCover 应该在 params 中,而不是在 files/values 中。您可能还需要将其设为 str(cover).lower(),因为它期待的是 true/false 而不是 True/False

# since you using a list of tuples:
params = (
    ('key',connection['trello_api_key']),('token',connection['trello_api_token']),('setCover',cover),)

或者作为字典:

params = {
    'key': connection['trello_api_key'],'token': connection['trello_api_token'],'setCover': cover,}