该项目是在 wangziyingwen/AutoApiSecret 项目的基础上修改而来。原项目不能将 refresh_token 信息进行隐藏,本项目通过使用 GitHub 的私有 gist 来存储 refresh_token 信息,达到了将 refresh_token 隐藏的目的。
如下图所示,创建私有 gist,****_token.txt
这个名称自拟。
在项目的 Secrets 中,创建 GIST_HTTPS_WITH_TOKEN、GIST_ID、GIST_TEXT、CONFIG_ID、CONFIG_KEY 五个机密。
- GIST_HTTPS_WITH_TOKEN 存的是 gist 的 HTTPS 形式链接(
https://gist.github.com/****gist-id****.git
),以用于 git clone,需要注意的是该链接需要携带 GitHub 账号的 access token 信息,在个人的Settings/Developer settings/Personal access tokens
中生成一个可以有创建 gist 权限的 token 即可。将 token 信息放到 gist 的 HTTPS 中,最后形式是https://****token****@gist.github.com/****gist-id****.git
。 - GIST_ID 存的是 gist 的 id 编号,即
https://****token****@gist.github.com/****gist-id****.git
中的****gist-id****
部分。 - GIST_TEXT 是 gist 中存放 token 的文件名称,包括文件后缀,如上图中的
****_token.txt
。 - CONFIG_ID 请直接填入你的应用 id 的值,而不需要像原项目中
id=r'你的应用id'
这样写,因为 main.py 中使用了os.environ
从环境变量中直接读取。 - CONFIG_KEY 与 4 同理,请直接填入你的应用机密的值,而不需要像原项目中的
secret=r'你的应用机密'
这样写。
在 GitHub Actions 的配置文件中(本项目是 .github/workflows/auto_invoke.yml
)中,通过 git clone $GIST_HTTPS
先将 gist 克隆下来(克隆下来的 gist 项目与 main.py 同目录),再在 main.py
中访问 ****_token.txt
。****_token.txt
的路径是通过当前路径、GIST_ID 和 GIST_TEXT 拼接而成的。
# 拼接存 refresh_token 的 gist 文件路径
filepath = Path.cwd() / os.environ["GIST_ID"] / os.environ["GIST_TEXT"]
当本次任务结束后,再将 gist 推送回 GitHub 上,由于之前 git clone 使用的 HTTPS 携带了 token,所以在填好用户名及邮箱后直接推送即可,如 yml
中所示。注意:由于 git clone 是在当前项目下生成的 gist 项目,故需要 cd 转移到 gist 文件夹内再推送。
- name: Push Gist # 上传新的 refresh_token 到 gist
env:
GIST_ID: ${{ secrets.GIST_ID }}
run: |
git config --global user.email "ClockworkRobot@email.com"
git config --global user.name "ClockworkRobot"
cd $GIST_ID && git add . && git commit -m "update new refresh_token" && git push origin master
参考了原项目中由 bindog 提交的 PR simplify the workflow and read the secret from environment variables #21 ,python 接口调用要携带的 id 和 secret 将直接从环境中获取。
yml 中向环境中添加相关变量
env:
CONFIG_ID: ${{ secrets.CONFIG_ID }}
python 从环境中获取相关变量
id = os.environ["CONFIG_ID"]
将相关调用的 API 链接放到列表中,并在每次执行循环调用前使用洗牌算法打乱顺序。
# API 链接列表
api_list = [
r'https://graph.microsoft.com/v1.0/me/drive/root',
r'https://graph.microsoft.com/v1.0/me/drive',
r'https://graph.microsoft.com/v1.0/drive/root',
r'https://graph.microsoft.com/v1.0/users',
r'https://graph.microsoft.com/v1.0/me/messages',
r'https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messageRules',
r'https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messageRules',
r'https://graph.microsoft.com/v1.0/me/drive/root/children',
r'https://graph.microsoft.com/v1.0/me/mailFolders',
r'https://graph.microsoft.com/v1.0/me/outlook/masterCategories'
]
# 洗牌,打乱顺序
random.shuffle(api_list)
同时还增加了随机时间,如 time.sleep(60 * random.randrange(1, 8))
和 time.sleep(random.randrange(2, 12))
以进行随机调用。