感谢您在文档中指出这一差距。下面的函数使用Requests Python 库为项目上传图像(该库使 multipart/form-data 请求变得更加简单)。请注意,如果您还没有安装 Requests ,则需要先安装。
import requests
def upload_item_image(item_id, image_path, access_token):
endpoint_path = 'https://connect.squareup.com/v1/' + your location + '/items/' + item_id + '/image'
# Don't include a Content-Type header, because the Requests library adds its own
upload_request_headers = {'Authorization': 'Bearer ' + access_token,
'Accept': 'application/json'}
# Be sure to set the correct MIME type for the image
files = [('image_data', (image_path, open(image_path, 'rb'), "image/jpeg"))]
response = requests.post(endpoint_path, files=files, headers=upload_request_headers)
# Print the response body
print response.text
item_id
是您为其上传图片的项目的 ID。
image_path
是您上传的图片的相对路径。
access_token
是您所代表的商家的访问令牌。
无法在单个请求中将多个项目的图像上传到此端点。相反,请为每个项目发送单独的请求。