Shopify API Python 使用 Python API 上传多张图片
Posted
技术标签:
【中文标题】Shopify API Python 使用 Python API 上传多张图片【英文标题】:Shopify API Python Multiple Pictures upload with Python API 【发布时间】:2018-11-02 09:12:24 【问题描述】:我正在尝试使用 Python API 在 Shopify 上添加多张图片,但是我无法将 2 张图片上传到一个产品。目前只有一张图片正在上传。如何向 Shopify API 添加多于 1 张图片?
import shopify
API_KEY = 'dsfsdsdsdsdsad'
PASSWORD = 'sadsdasdasdas'
shop_url = "https://%s:%s@teststore.myshopify.com/admin" % (API_KEY, PASSWORD)
shopify.ShopifyResource.set_site(shop_url)
path = "audi.jpg"
path2 = "audi2.jpg"
new_product = shopify.Product()
new_product.title = "Audi pictures test "
new_product.body_html = "body of the page <br/><br/> test <br/> test"
variant = shopify.Variant('price': 1.00, 'requires_shipping': False,'sku':'000007')
new_product.variants = [variant]
image = shopify.Image()
image2 = shopify.Image()
with open(path, "rb") as f:
filename = path.split("/")[-1:][0]
filename2 = path2.split("/")[-1:][0]
encoded = f.read()
image.attach_image(encoded, filename=filename)
image2.attach_image(encoded, filename=filename2)
new_product.images = [image,image2]
new_product.save()
【问题讨论】:
【参考方案1】:我没有 shopify 帐户来验证以下代码,但我查看了源代码,下面是适合您的方法
import shopify
API_KEY = 'dsfsdsdsdsdsad'
PASSWORD = 'sadsdasdasdas'
shop_url = "https://%s:%s@teststore.myshopify.com/admin" % (API_KEY, PASSWORD)
shopify.ShopifyResource.set_site(shop_url)
path = "audi.jpg"
path2 = "audi2.jpg"
new_product = shopify.Product()
new_product.title = "Audi pictures test "
new_product.body_html = "body of the page <br/><br/> test <br/> test"
variant = shopify.Variant('price': 1.00, 'requires_shipping': False,'sku':'000007')
new_product.variants = [variant]
success = new_product.save()
if success:
product_id = new_product.id
image = shopify.Image("product_id": product_id)
image2 = shopify.Image("product_id": product_id)
filename = path.split("/")[-1:][0]
filename2 = path2.split("/")[-1:][0]
encoded = f.read()
image.attach_image(encoded, filename=filename)
image2.attach_image(encoded, filename=filename2)
image.save()
image2.save()
想法是创建产品,然后附加图像
【讨论】:
以上是关于Shopify API Python 使用 Python API 上传多张图片的主要内容,如果未能解决你的问题,请参考以下文章
使用 Shopify API Python,添加带有价格和“requires_shipping”的新产品:False
Shopify - 使用 Shopify API 的新订单 - 如何了解税费和运费?
使用 Shopify API 获取当前产品 ID 以进行 Javascript API 调用