定义一个函数获取access_token

def getacctoken(): # 输出信息 print('getoken') # 构建请求access_token的url access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential'
'&appid={appid}&secret={secret}'.format(appid=APPID, secret=SECRET) # 发送请求并获取access_token access_token_res = requests.get(access_token_url).json()['access_token'] # 查询数据库中id为2的记录 atok = Adj.query.filter(Adj.id == 2).first() # 如果存在,则更新记录中的adjinfo为获取的access_token if atok: atok.adjinfo = access_token_res # 如果不存在,则创建一条新的记录 else: atok = Adj(id = 2,adjinfo=access_token_res) db.session.add(atok) # 提交更改到数据库中 db.session.commit() # 返回获取的access_token return access_token_res # 如果获取access_token的请求返回了错误码,则抛出异常 if access_token_res.json().get('errcode'): raise 'AccessToken()'

定义一个路由为/test的函数

@app.route('/test',methods=['POST']) def test(): # 调用infocheck函数,并将返回值赋值给a a = infocheck(text,openid) # 返回a return a

def getacctoken printgetroken access_token_url = httpsapiweixinqqcomcgi-bintokengrant_type=client_credential &appid=appid&secret=secretformatappid=APPID secret=SECRET a

原文地址: https://www.cveoy.top/t/topic/H3w 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录