You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Etsy API文件上传报400(需提供有效文件名)求解决方法

问题描述

我正在使用Etsy API完成商品列表创建、图片上传及数字产品文件下载操作,目前商品列表创建和图片上传功能已正常实现,但数字产品文件上传功能出现异常。按照官方文档说明,文件上传流程和图片上传类似,但实际调用无法成功。

使用的上传函数代码如下:

def upload_file(keystring,token,shop_id,listing_id,file):
  
    headers = {
        "Accept": "application/json",
        "x-api-key": keystring,
        "Authorization" : f"Bearer {token}"
              }
    
    url = f"https://openapi.etsy.com/v3/application/shops/{shop_id}/listings/{listing_id}/files"
    
    files = {'file': ('10.png', open(file,'rb'))}
    

    response = requests.post(
        url=url,
        headers=headers,
        files=files
    )

    if response.status_code == 200:
        print(response.json())
    else:
        print('Upload failed with status code:', response.status_code) 
        print(response.json()) 

调用后返回状态码400,错误信息为:A valid name must be provided with a new file.

排查思路与修复方案

错误原因

错误提示明确指出必须为新文件提供有效的名称,说明当前请求缺少name表单参数。图片上传仅需传递文件本身,但数字产品文件上传需要额外指定该参数。

修复代码

调整请求,添加name字段,同时保留文件上传部分:

import os

def upload_file(keystring, token, shop_id, listing_id, file_path):
    headers = {
        "Accept": "application/json",
        "x-api-key": keystring,
        "Authorization": f"Bearer {token}"
    }
    
    url = f"https://openapi.etsy.com/v3/application/shops/{shop_id}/listings/{listing_id}/files"
    
    # 获取本地文件名作为Etsy后台显示的文件名
    file_name = os.path.basename(file_path)
    # 构造表单数据,包含必填的name字段
    form_data = {'name': file_name}
    # 构造文件上传参数
    files = {'file': (file_name, open(file_path, 'rb'))}
    
    response = requests.post(
        url=url,
        headers=headers,
        data=form_data,
        files=files
    )

    if response.status_code == 200:
        print(response.json())
    else:
        print('Upload failed with status code:', response.status_code)
        print(response.json())

额外注意点

  • 必须导入os模块来获取本地文件名;
  • name参数的值需符合Etsy的规范,避免特殊字符或过长;
  • 确认上传文件符合Etsy数字产品的格式、大小限制。

内容的提问来源于stack exchange,提问作者Jānis Š

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:05:17