【问题标题】:What is the reason for failing program to work after calling account.authenticate() and re-running with the `authenticate()` commented?调用 account.authenticate() 并重新运行并注释 `authenticate()` 后程序无法运行的原因是什么?
【发布时间】:2022-09-23 23:30:49
【问题描述】:

链接:https://github.com/O365/python-o365

调用 account.authenticate。此调用将为您请求一个令牌,并且 将其存储在后端。不需要用户交互。方法 将令牌存储在后端并返回 True 如果 认证成功。

我在 python 中创建了一个后台服务,它使用 python 和 O365 发送电子邮件。

当我运行以下命令时,它给了我一个令牌丢失的错误:

from O365 import Account  
credentials = (\'my_client_id\', \'my_client_secret\')    
account = Account(credentials, auth_flow_type=\'credentials\', tenant_id=\'my-tenant-id\')
# if account.authenticate():
#     print(\'Authenticated!\')
mailbox = account.mailbox(\'sender@mailbox.com\') 
inbox = mailbox.inbox_folder()
m = mailbox.new_message()
m.to.add(\'recep@mailbox.com\')
m.subject = \'My subject\'
m.body = \'My email\'
m.send()

当我添加.authenticate 时,如下所示,发送电子邮件就可以了。

from O365 import Account  
credentials = (\'my_client_id\', \'my_client_secret\')    
account = Account(credentials, auth_flow_type=\'credentials\', tenant_id=\'my-tenant-id\')
if account.authenticate():
    print(\'Authenticated!\')
    mailbox = account.mailbox(\'sender@mailbox.com\') 
    inbox = mailbox.inbox_folder()
    m = mailbox.new_message()
    m.to.add(\'recep@mailbox.com\')
    m.subject = \'My subject\'
    m.body = \'My email\'
    m.send()

当我评论以下行 .authenticate 并运行它的程序时:

from O365 import Account  
credentials = (\'my_client_id\', \'my_client_secret\')    
account = Account(credentials, auth_flow_type=\'credentials\', tenant_id=\'my-tenant-id\')
# if account.authenticate():
#     print(\'Authenticated!\')
mailbox = account.mailbox(\'sender@mailbox.com\') 
inbox = mailbox.inbox_folder()
m = mailbox.new_message()
m.to.add(\'recep@mailbox.com\')
m.subject = \'My subject\'
m.body = \'My email\'
m.send()

为什么程序一开始就失败了,最后还是同一个程序工作了?

    标签: python python-3.x python-o365


    【解决方案1】:

    Il semblerait que ça s'appuie sur les sessions :

    https://github.com/O365/python-o365/blob/a1022bc5ece43e4b15c3e57a5efaa810dcdedebf/O365/message.py#L713

    ichttps://github.com/O365/python-o365/blob/a1022bc5ece43e4b15c3e57a5efaa810dcdedebf/O365/connection.py#L823

    Ce qui est marrant, c'est le pattern parent quand on suit ton 指令

    mailbox = account.mailbox('sender@mailbox.com') 
    

    https://github.com/O365/python-o365/blob/a1022bc5ece43e4b15c3e57a5efaa810dcdedebf/O365/account.py#L162

    https://github.com/O365/python-o365/blob/a1022bc5ece43e4b15c3e57a5efaa810dcdedebf/O365/mailbox.py#L40

    Puis c'est encore le meme pattern avec l'instruction :

    m = mailbox.new_message()
    

    https://github.com/O365/python-o365/blob/a1022bc5ece43e4b15c3e57a5efaa810dcdedebf/O365/mailbox.py#L478

    En gros la class Message est enfant de MailBox (avec MailBox héritant de Folder) et MailBox est enfant de Account。 La connexion (self.con) est initialisé par Account

    Je sais pas si ça répond à ta question?

    加 d'info ici : https://requests.readthedocs.io/en/latest/user/advanced/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多