微信公众号接口添加菜单时错误(errcode”:40017 invalid button type)
POST提交时总是报错:
{"errcode":40017,"errmsg":"invalid button type"}
最后查出来是由于数据中有中文引起的
解决:
data = {"button":[
{"name": u"会员服务",
"sub_button":[
{"type":"click","name":u"健康咨询","key":"JKZX"},
]
},
{"name":u"申请加入","sub_button":[
{"type":"view","name":u"企业入会申请","url": ""},
]
},
{"type":"view","name":u"关于","url":""}
]
}
url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=。。。'
data = simplejson.dumps(data, ensure_ascii=False).encode('utf-8')
#加上参数ensure_ascii=False 后 提交的数据中的中文就不会再被转码,然后再编下UTF-8
print data
req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')
req.add_header('encoding', 'utf-8')
response = urllib2.urlopen(req, data)
result = response.read()
print result
小猪CMS在提交“生成自定义菜单”的时候,提示“发生错误:错误代码40017,微信返回错误信息:不合格的按钮个数”。但是网站本身用的就是UFT-8的格式,应该不需要以上方式去转码,仔细检查“\PigCms\Lib\Action\User\DiymenAction.class.php”发现编码是“UTF-8”格式,立即转码为“UTF-8 w/o BOM”格式,再试一遍,正常提交!