【问题标题】:Uploading Item Image using Square Connect API使用 Square Connect API 上传商品图片
【发布时间】:2018-04-03 01:20:31
【问题描述】:

我查看了 Square Connect API 文档中发布的示例和 GitHub 上的示例,但是,我似乎无法将这些示例改编为上传图片的指南:http://docs.connect.squareup.com/#post-image

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

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

【问题讨论】:

    标签: python square-connect


    【解决方案1】:

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

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

    【讨论】:

    • 感谢斯蒂芬的回答。不幸的是,我无法让它工作。当我执行代码时,它只是运行而没有任何事情发生。我应该补充一点,我确实在最后一行代码中遇到了与括号相关的语法错误,我通过将其更改为:print (response.text()) 来解决。您在代码中是否看到任何需要修改才能使其正常工作的内容?提前感谢您的帮助!
    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多