-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.py
80 lines (73 loc) · 3.02 KB
/
run.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: UTF-8 -*-
#先注册azure应用,确保应用有以下权限:
#files: Files.Read.All、Files.ReadWrite.All、Sites.Read.All、Sites.ReadWrite.All
#user: User.Read.All、User.ReadWrite.All、Directory.Read.All、Directory.ReadWrite.All
#mail: Mail.Read、Mail.ReadWrite、MailboxSettings.Read、MailboxSettings.ReadWrite
#注册后一定要再点代表xxx授予管理员同意,否则outlook api无法调用
import requests as req
import json,sys,time,os,random
from urls import *
num = 0
path = os.getcwd() + r'/token.txt'
def update_token(refresh_token):
headers={'Content-Type':'application/x-www-form-urlencoded'}
data= {'grant_type': 'refresh_token',
'refresh_token': refresh_token,
'client_id':os.environ.get('client_id'),
'client_secret':os.environ.get('client_secret'),
'redirect_uri':'http://localhost:53682/'
}
html = req.post('https://login.microsoftonline.com/common/oauth2/v2.0/token', data=data,headers=headers)
jsontxt = json.loads(html.text)
access_token = jsontxt['access_token']
refresh_token = jsontxt['refresh_token']
return access_token, refresh_token
def load_token():
with open(path, "r") as fo:
refresh_token = fo.read()
try:
REDIS_HOST = os.environ.get('redis_host', '')
REDIS_PORT = int(os.environ.get('redis_port', 0))
REDIS_PASSWORD = os.environ.get('redis_password', '')
import redis
with redis.Redis(host=REDIS_HOST,port=REDIS_PORT,password=REDIS_PASSWORD) as red:
if red.exists(os.environ.get('client_id')):
refresh_token = red.get(os.environ.get('client_id'))
else:
red.set(os.environ.get('client_id'), refresh_token)
access_token, refresh_token = update_token(refresh_token)
red.set(os.environ.get('client_id'), refresh_token)
return access_token
except Exception as e:
print(e)
print("连接Redis出错,尝试使用本地refresh_token")
access_token, refresh_token = update_token(refresh_token)
try:
with open(path, "w") as fo:
fo.write(refresh_token)
print("尝试保存refresh_token到文件")
except Exception as e:
print(e)
print("保存refresh_token到文件错误")
return access_token
def add_count(type=""):
global num
num+=1
print(type + "调用成功"+str(num)+'次')
time.sleep(random.randint(0,10))
def main():
time.sleep(random.randint(0,30))
access_token = load_token()
headers={'Authorization':access_token,'Content-Type':'application/json'}
try:
for name,url in urls.items():
if req.get(url,headers=headers).status_code == 200:
add_count(name)
except:
pass
def main_handler(event, context):
print("任务开始")
for _ in range(random.randint(1,3)):
main()
localtime = time.asctime(time.localtime(time.time()))
print('此次运行结束时间为 :', localtime)