gpt4 book ai didi

python - 使用 Square Connect API 上传商品图像

转载 作者:行者123 更新时间:2023-12-01 04:53:25 25 4
gpt4 key购买 nike

我已经查看了 Square Connect API 文档中发布的示例以及 GitHub 上的示例,但是,我似乎无法使这些示例适应上传图像的指南:http://docs.connect.squareup.com/#post-image

挑战的一部分是使用 Content-Type: multipart/form-data,只有图像上传需要,因此文档不存在(使用 connect-api 文档)。

我的最终问题是,Square 能否发布一个如何上传图片的示例?最相关的是一个示例,展示如何使用图像更新多个项目而不是仅更新一个项目。如有任何帮助,我们将不胜感激。

最佳答案

感谢您指出文档中的这一空白。下面的函数使用 Requests用于上传项目图像的 Python 库(该库使多部分/表单数据请求变得更加简单)。请注意,您需要 install 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 是您所代表的商家的访问 token 。

无法在对此端点的单个请求中上传多个项目的图像。相反,请为每个项目发送单独的请求。

关于python - 使用 Square Connect API 上传商品图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27978978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com