python - 微信公众平台上传素材遇到的问题
PHPz
PHPz 2017-04-17 17:31:26
0
1
1111

最近写了图片相关的应用放在 SAE 上,应用的逻辑简单的说就是生成满足用户特定要求的图片,然后直接以图片消息的形式发送给用户微信。应用中首次生成图片后会同时依据图片的特征参数生成一个唯一的 KEY ,与调用上传永久素材接口后得到的 mediaid 组成一个键值对存入SAE kvdb库,如果上传永久素材出错,则尝试调用上传临时素材的接口得到一个有效期较短的 mediaid (文档说是 3 天),最后利用获得的 mediaid 和用户 openid 把图片通过客服消息接口发给用户,然后定期清理数据库和微信永久素材库,保留最新的调用频次高的图片。

前两天用的都很正常,前天晚上发现上传素材时老是报错,没太在意,昨天早上我试了下又好了,就以为是服务器的问题。昨天下午又出错,查了下发现错误代码是: 41005,media data missing,又单独测试了上传临时素材接口发现没问题,接着测上传永久素材接口,报错: 45009,reach max api daily limit ,超过了每日上限,问题来了:

  1. 我微信端虽然暂时使用的是测试号,常见的接口每日调用的上限好像更少,但我没调用多少次。我查了下测试号个人页面的介绍,调用上传永久素材接口的每日上限为1000 ,临时素材为 5000 ,但我显然没有调用这么多次数,查了 sae上的日志,今天一天应用访问次数也就一百多次,微信永久素材库上传的图片也就 40张,就已经超过调用上限了。。。今天上午测试又恢复了,实在想不通,咨询客服也无果。

  2. 我上传临时素材的接口单独测试没有问题,为啥代码里如果在上传永久素材失败后,紧接着调用上传临时素材接口也会报错? 我现在只能临时把上传永久素材的代码全部去掉,全部通过上传临时素材的接口上传图片获mediaid,目前工作正常。如果要使用上传永久素材接口,必须每天手动修改代码,否则一旦超过次数,上传临时素材接口也报错,不知有什么好的办法。

请各位有微信公众平台开发经验的朋友指点迷津,多谢!
上述过程用 python 代码片段简要的描述下:

  ...
  io = render_img(...) #生成图片存储在一个StringIO对象中
  (mediaid,errmsg) = wx.add_material(io) #45009 reach max api daily limit 
  if mediaid:
       value = "%s %s" %(mediaid,str(int(time.time())))
       kv.set(k,value)
       return (mediaid,errmsg)
  (mediaid,errmsg) = wx.upload_img(io) #41005,media data missing ,去掉上面的 add_material和if判断,则正常
  return (mediaid,errmsg)

上传图片使用的都是requests的post方法:

...
token = getAccesstoken()
files = { 'media' : ('tmp.png',rawimg,'image/png')}
url = ...  #微信上传素材的url 替换成得到的token
res = requests.post(url, files=files)
Dict = res.json()
...
PHPz
PHPz

学习是最好的投资!

reply all(1)
Peter_Zhu

1. The upper limit of the WeChat test account calling the upload permanent material interface is about ten times per day
2. After the permanent material upload fails, the reason for the error in uploading temporary materials is that io can no longer be passed directly as a parameter in the code to the function that uploads temporary materials. wx.upload_img, after many experiments, it was found that the safe way is to release the io generated when uploading permanent materials, regenerate a stringIO object to save the image, and then call the upload wx.upload_img function.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!