简体   繁体   English

使用 Python 和 REST API 将图片上传到 Shopify

[英]Upload image to Shopify using Python and REST API

I have read through the Shopify API documentation for how to insert an image to Shopify from here: https://shopify.dev/docs/admin-api/rest/reference/products/product-image我已阅读 Shopify API 文档,了解如何从此处将图像插入 Shopify: https ://shopify.dev/docs/admin-api/rest/reference/products/product-image

I have the following code, which can successfully connect to Shopify and retrieve images associated with a product:我有以下代码,它可以成功连接到 Shopify 并检索与产品关联的图像:

import requests

# Connect to Shopify
shop_url = "https://%s:%s@store.myshopify.com/admin/api/%s/products/5161320611973/images.json" % (API_KEY, PASSWORD, API_VERSION)

# Get data
data = requests.get(shop_url)

This works successfully, Now I would like to upload a new image to a product.这成功了,现在我想将新图像上传到产品。 This is the code I am using:这是我正在使用的代码:

# Create image data
imgdata = {
  "image": {
    "src": "https://pkmncards.com/wp-content/uploads/en_US-CP-079-charizard_v.jpg"
  }
}

# POST image data to Shopify
x = requests.post(shop_url, data = imgdata)

This gives the following result:这给出了以下结果:

<Response [400]>
{'errors': {'image': 'expected String to be a Hash'}}

Does anyone have a suggestion as to what mistake I may have made?有没有人对我可能犯的错误有什么建议?

Using json instead of data in the requests should do the trick:在请求中使用json而不是data应该可以解决问题:

# POST image data to Shopify
x = requests.post(shop_url, json = imgdata)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM