Skip to main content
 首页 » 编程设计

python之如何保存 token 以便在 O365 python 库中重复使用

2024年11月24日9leader

我正在使用 python O365 库阅读和发送我的 office 365 帐户的邮件,以自动执行一些常规任务。每次我想使用这个 API 时,我都必须进行验证并获得一个新的 token ,这个 token 每 60 分钟过期一次。因此,经过深入研究后,我发现了一种名为 FileSystemTokenBackend 的方法,但我仍然无法保存 token 。这就是我试图保存的方式

token_backend = O365.FileSystemTokenBackend(token_path='G:/Newfolder', token_filename='my_token.txt') 

即使在这个命令之后,也没有 token 被保存。

请您参考如下方法:

根据docs ,您的 token 将存储在指定路径当您进行身份验证时。

首次认证后,将在指定路径读取token。当您使用指向此路径的 token 后端创建帐户时,此帐户将自动登录。

token_backend = FileSystemTokenBackend(token_path='token_dir', token_filename='o365_token.txt') 
account = Account(CREDENTIALS, token_backend=token_backend) 
# If it's your first login, you will have to visit a website to authenticate and paste the redirected URL in the console. Then your token will be stored. 
# If you already have a valid token stored, then account.is_authenticated is True. 
if not account.is_authenticated: 
    account.authenticate(scopes=['basic', 'message_all'])