微信公众号开发(三) 获取全局access_token

调用微信的接口都需accss_token, 微信有很多access_token,这里讲的access_token是全局唯一的,不同于微信网页授权中的access_token.

get_global_access_token.py

url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}". \
            format(APP_ID, APP_SECRET)
resp = requests.get(url)
content = resp.json()
errcode = content.get("errcode")
print("content: {}".format(content))
if errcode in content:
   errmsg = content.get("errmsg")
   print("获取access_token失败:{}".format(errmsg))
   raise ValueError("获取access_token失败:{}".format(errmsg))
else:
   access_token = content.get("access_token")
   expires_in = content.get("expires_in")

access_token的有效期是2个小时,所以需要把access_token存在服务器上,写个定时任务,定时刷新

你可能感兴趣的:(微信公众号开发,python,微信公众号)