有赞新零售社区

发帖
API使用问题»素材上传问题

素材上传问题

b5622249eda4 2019-10-24 1890 浏览 4 评论 | [显示全部楼层] [打印]
Hi,
我们之前的集成是基于 有赞开放平台 做的,当时用的你们提供的 Python SDK。现在绑定新店铺发现只支持 有赞云 了,两边的接口稍稍有些不同。我已经稍作修改了原先的 Python SDK(因为有赞云版本API没有提供对应的 Python SDK),调试通过了 认证和 GET 请求,但是现在卡在一个上传图片接口 POST请求 youzan.materials.storage.platform.img.upload  https://doc.youzanyun.com/doc#/content/API/1-372/detail/api/0/193   。为了方便调试,我已经抽取出我们的请求为 curl 命令,如下(我把token隐去了):

# 直接填空的
➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/json;charset=UTF-8" -d "{}"   https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_
{"code":160400100,"success":false,"request_id":null,"message":"file参数错误"}

➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/json;charset=UTF-8" -d "{\"\":\"\"}"   https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_token\=123
{"code":160400100,"success":false,"request_id":null,"message":"file参数错误"}

# 填了 image 字段,但是类型是错误的字符串
➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/json;charset=UTF-8" -d "{\"image\":\"\"}"   https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_token\=123
{"gw_err_resp":{"err_code":5001,"err_msg":"系统异常"}}

# 填了正确 image 字段, 无字段名
➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/json;charset=UTF-8" --data-binary "@Pictures/BritishBrands.jpg"  https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_token\=123
{"gw_err_resp":{"err_code":5001,"err_msg":"系统异常"}}

# 填了正确 image 字段, 有字段名
➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/json;charset=UTF-8" -F "image=@Pictures/BritishBrands.jpg"  https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_token\=123
{"gw_err_resp":{"err_code":5001,"err_msg":"系统异常"}}

# 故意填错文件路径
➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/json;charset=UTF-8" -F "image=@Pictures/BritishBrands.jp"  https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_token\=123
curl: (26) couldn't open file "Pictures/BritishBrands.jp"

# 改成另一种方式的字段名,支持数组方式
➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/json;charset=UTF-8" -F "image[]=@Pictures/BritishBrands.jpg"  https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_token\=123
{"gw_err_resp":{"err_code":5001,"err_msg":"系统异常"}}

# 正确 form header
➜  ~ curl -H "User-Agent: X-YZ-Client 2.0.0 - Python;" -H "Content-Type:application/x-www-form-urlencoded" -F 'image=@Pictures/BritishBrands.jpg'    https://open.youzanyun.com/api/y ... rm.img.upload/3.0.0\?access_token\=123
{"gw_err_resp":{"err_code":5001,"err_msg":"系统异常"}}




麻烦帮忙解答一下,万分感谢!
用手机打开
收藏 ··· 回复
    admin 发表于 2019-10-24 09:44
    请严格按照有赞云的接口调用规则去调用,使用application+post方式;
    上传图片接口的要求具体看文档要求。 ...


    你好,我上面的所有 CURL 请求都是 application+post 啊,实在调试不出来了,还请抽时间帮忙指点一下,谢谢啦

      admin 发表于 2019-10-24 17:35
      这个接口上传图片的格式:本地图片文件byte流,仅支持单个文件,每个文件不超过1MB。
      目前没有python示例, ...


      好的,感谢,终于调通了,Python 版本,希望可以帮到其他遇到同样问题的人。

      1. import requests

      2. url = "https://open.youzanyun.com/api/youzan.materials.storage.platform.img.upload/3.0.0?access_token=123"

      3. img_file = open("/Users/dev/Pictures/BritishBrands.jpg", "rb")
      4. files = [('image', ('image', img_file))]

      5. print(requests.post(url, files=files).text)
      复制代码


      有点 Tricky。应该用 requests 的 files 参数,而且得不是 Python tempfile。

        1跳至
        您需要登录后才可以回帖 登录 | 立即注册

        本版积分规则

        复制链接
        新浪微博
        QQ空间
        微信扫码
        • 回复

        • 评分

        客服工作时间是9:00-18:00,客服妹子当前不在线,若不能及时回复请谅解。试试右上角的搜索吧,论坛有丰富的经验贴、公告贴,相信一定能够帮到您~

        复制成功