-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_token.py
39 lines (34 loc) · 1.06 KB
/
access_token.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 获取和管理access_token
import aiohttp
import aiofiles
import asyncio
import json
import time
token = ""
expire = 0
lock = asyncio.Lock()
with open("data/secrets.json") as f:
secrets = json.load(f)
async def query_access_token():
"向服务器查询access token"
url = "https://api.weixin.qq.com/cgi-bin/token"
params = {
"grant_type": "client_credential",
"appid": secrets["AppId"],
"secret": secrets["AppSecret"]
}
resp = await aiohttp.get(url, params=params)
data = await resp.json()
return data
async def get_access_token():
"若未过期则返回原来的access token,否则获取新的token"
global token, expire
async with lock:
if time.time() > expire:
data = await query_access_token()
async with aiofiles.open("token.json", "wt") as f:
await f.write(json.dumps(data))
assert 'access_token' in data and 'expires_in' in data
token = data['access_token']
expire = time.time() + data['expires_in']
return token