Skip to content

Instantly share code, notes, and snippets.

@zhicheng
Created August 7, 2013 07:10
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 zhicheng/6171865 to your computer and use it in GitHub Desktop.
Save zhicheng/6171865 to your computer and use it in GitHub Desktop.
# encoding: utf-8
import json
import urllib
import urllib2
appid = "xxx"
secret = "xxx"
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": "XXX",
"type": "click",
"key": "XXX"
},
{ "name": "XXX",
"type": "click",
"key": "XXX"
},
{
"name": "XXX",
"type": "click",
"key": "XXX"
}]}
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