使用Python API上传Shopify API Python多个图片

ben*_*sen 10 python api image shopify

我试图用Shop 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()
Run Code Online (Sandbox Code Playgroud)

Tar*_*ani 13

我没有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()
Run Code Online (Sandbox Code Playgroud)

想法是创建产品然后附加图像