1

我已经查看了 Square Connect API 文档中发布的示例和 GitHub 上的示例,但是,我似乎无法将这些示例调整为上传图像的指南:http: //docs.connect.squareup.com/#后图像

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

我的终极问题是,Square 能否发布一个如何上传图片的示例?最相关的是一个示例,该示例显示了如何使用图像更新多个项目而不是仅更新一个项目。任何帮助表示赞赏。

4

1 回答 1

2

感谢您在文档中指出这一差距。下面的函数使用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是您所代表的商家的访问令牌。

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

于 2015-01-21T22:51:14.690 回答