Skip to content

Instantly share code, notes, and snippets.

@hzlzh
Last active January 20, 2016 03:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hzlzh/6332608 to your computer and use it in GitHub Desktop.
Save hzlzh/6332608 to your computer and use it in GitHub Desktop.
WeiXIn Menu Post
# encoding: utf-8
import json
import urllib
import urllib2
appid = ""
secret = ""
access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create?%s"
def get_access_token():
f = urllib.urlopen(access_url % (appid, secret))
resp = json.loads(f.read())
print resp
return resp['access_token']
def generate_menu(token):
menus = {"button": [
{
"name": "最新帖",
"type": "click",
"key": "n"
}]
params = {'access_token': urllib.quote(token)}
url = menu_url % urllib.urlencode(params)
request = urllib2.Request(url, json.dumps(menus, ensure_ascii=False))
response = urllib2.urlopen(request)
print response.read()
def main():
token = get_access_token()
generate_menu(token)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment