O365无法导入邮箱

O365 cant import Mailbox

我正在为一个项目测试 O365,我必须为其下载一些附件。我只是试图连接到服务器并了解如何使用此模块。我什至无法让导入正常工作。我已经在线学习了一些教程并查看了文档并且有邮箱选项,所以我不确定问题是什么。有什么建议吗?

(project2) User1@User1-MacBook-Pro project2 % python3 main.py
Traceback (most recent call last):
  File "/Users/User1/Documents/scripts/Python/project2/main.py", line 2, in <module>
    from O365 import Account, Message, MailBox
ImportError: cannot import name 'MailBox' from 'O365' (/Users/User1/Documents/scripts/Python/project2/lib/python3.9/site-packages/O365/__init__.py)
(project2) User1@User1-MacBook-Pro project2 % pip3 show o365
Name: O365
Version: 2.0.16
Summary: Microsoft Graph and Office 365 API made easy
Home-page: https://github.com/O365/python-o365
Author: Janscas, Roycem90, Narcolapser
Author-email: janscas@users.noreply.github.com
License: Apache License 2.0
Location: /Users/User1/Documents/scripts/Python/project2/lib/python3.9/site-packages
Requires: pytz, tzlocal, requests-oauthlib, stringcase, beautifulsoup4, requests, python-dateutil
Required-by:
from O365 import Account, Message, MailBox

scopes=['basic', 'message_all']
credentials=('extra_secure_password', 'user@somedomain.com')
account = Account(credentials = credentials)

# if not account.is_authenticated:  # will check if there is a token and has not expired
#     account.authenticate(scopes=scopes)

# account.connection.refresh_token().mailbox = account.mailbox()
inbox = mailbox.get_folder(folder_name='Inbox')
child_folders = inbox.get_folders(25)
for folder in child_folders:
    print(folder.name, folder.parent_id)

嘿,也许你必须走这条路

from O365 import Connection, MSGraphProtocol
from O365.message import Message
from O365.mailbox import MailBox

protocol = MSGraphProtocol()
scopes = ['...']
con = Connection(('client_id', 'client_secret'), scopes=scopes)

message = Message(con=con, protocol=protocol)
# ...
mailbox = MailBox(con=con, protocol=protocol)
message2 = Message(parent=mailbox)  # message will inherit the connection and protocol from mailbox when using parent.
# ...

你的错误在这一行:

ImportError: cannot import name 'MailBox' from 'O365'

你必须这样写:

from O365.mailbox import MailBox

要查找完整文章,请转到此页面 - https://github.com/O365/python-o365#mailbox