【问题标题】:how to create an image _FILES array using python to upload image如何使用python创建图像_FILES数组来上传图像
【发布时间】:2020-07-09 10:40:39
【问题描述】:

我正在创建一个脚本来使用 python 将列表图像上传到 etsy。

但是,当我执行它时,它会返回一条消息 "图像数组元数据看起来不像 _FILES 数组"

顺便说一句,我正在使用库https://github.com/mcfunley/etsy-python

我试过这段代码:

product_image = open(filename)

result = etsy.uploadListingImage(listing_id=listing_id, image=product_image)

但它会返回错误消息“UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte”

我又试了一个:

product_image = {filename, open(filename, 'rb'), 'image/jpeg'}

result = etsy.uploadListingImage(listing_id=listing_id, image=product_image)

但它会返回错误消息“图像数组元数据看起来不像 _FILES 数组”

有一个用于 php (https://www.etsy.com/developers/documentation/getting_started/images#section_uploading_images) 的示例代码,但我很难将它与我使用的 python 库保持一致。

$source_file = dirname(realpath(__FILE__)) ."/$filename";

$url = "https://openapi.etsy.com/v2/listings/".$listing_id."/images";
$params = array('@image' => '@'.$source_file.';type='.$mimetype);

$oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST);

$json = $oauth->getLastResponse();

所以我认为主要问题是uploadListingImage的图像参数的变量类型或结构。

【问题讨论】:

    标签: python python-3.x image file etsy


    【解决方案1】:

    这样的事情应该可以工作:

    from requests_oauthlib import OAuth1Session
    
    etsy = OAuth1Session(
        api_key,
        shared_secret,
        oauth_token, 
        oauth_token_secret
    )
    
    url = f'https://openapi.etsy.com/v2/listings/{listing_id}/images'
    files = { 'image': (open(image_path, 'rb') }
    
    response = etsy.post(url, files = files)
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多