【问题标题】:Grabbing files from Microsoft Teams using Python使用 Python 从 Microsoft Teams 抓取文件
【发布时间】:2019-07-18 22:58:33
【问题描述】:

Teams 似乎缺乏将文件镜像到共享目录的任何本机方式。我正在尝试使用 Python(或其他语言,但首选 Python!):

一个。使用 Python 直接从微软团队拉入内存,用 Pandas 处理

b.将团队中的文件复制到共享网络文件夹(Python 可以随后读取)

我发现了这个,但无法让它与团队一起使用 - 团队 URL 看起来不像这些。 How to read SharePoint Online (Office365) Excel files in Python with Work or School Account?

这似乎接近我想要做的。我还在 PyPi 存储库上找到了“pymsteams”。 https://pypi.org/project/pymsteams/ 似乎只是让您向 Teams 发送消息而不是其他任何东西?除非我误解了什么。

https://pypi.org/project/Office365-REST-Python-Client/

https://pypi.org/project/pymsteams/

from office365.runtime.auth.authentication_context
import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File 

url = 'https://teams.microsoft.com/l/file'
username = 'myusername'
password = 'mypassword'
relative_url ='myurl'

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)

尝试运行上面的代码给出 AttributeError: 'NoneType' 对象没有属性 'text'

完整的堆栈跟踪:

runfile('H:/repos/foo/untitled0.py', wdir='H:/repos/foo')
Traceback (most recent call last):

  File "<ipython-input-35-314ab7dc63c9>", line 1, in <module>
    runfile('H:/repos/foo/untitled0.py', wdir='H:/foo/image_ai')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "H:/repos/image_ai/untitled0.py", line 10, in <module>
    ctx_auth.acquire_token_for_user(username, password)

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\authentication_context.py", line 18, in acquire_token_for_user
    return self.provider.acquire_token()

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\saml_token_provider.py", line 57, in acquire_token
    self.acquire_service_token(options)

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\saml_token_provider.py", line 88, in acquire_service_token
    token = self.process_service_token_response(response)

  File "C:\ProgramData\Anaconda3\lib\site-packages\office365\runtime\auth\saml_token_provider.py", line 119, in process_service_token_response
    return token.text

AttributeError: 'NoneType' object has no attribute 'text'

【问题讨论】:

  • 你能显示整个堆栈跟踪吗?
  • 谢谢,我添加了完整的堆栈跟踪。
  • 对我来说似乎是一个身份验证错误
  • 所有 Microsoft Teams 文件都存储在 SharePoint 上,因此您应该使用 Microsoft Graph APIs for OneDrive and SharePoint。这是Send and receive files through your bot 上的文档。
  • 机器人仅适用于个人聊天,不适用于频道中发布的文件。

标签: python-3.x office365 office365api microsoft-teams


【解决方案1】:

我已经设法使用您链接的 Office-365-REST-Python-Client 使其工作。

如果您使用的是 SharePoint Online,则需要设置 App-Only principle 并使用 acquire_token_for_app 函数而不是 acquire_token_for_user 进行连接,然后传递 client_id 和 client_secret 而不是用户名和密码。

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File

client_id = 'yourclientid'
client_secret = 'yourclientsecret'
url = 'https://yoursharepointsite.com/teams/yourteam'
relative_url = '/teams/yourteam/Shared%20Documents/yourteamschannel/yourdoc.extension'
  
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_app(client_id, client_secret):
    ctx = ClientContext(url, ctx_auth)
    with open(filename, 'wb') as output_file:
        response = File.open_binary(ctx, relative_url)
        output_file.write(response.content) 
else:
    print(ctx_auth.get_last_error())

这应该将您的文件下载到本地驱动器(通过文件名变量指定),然后您可以加载到 pandas 等进行处理

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 2020-10-05
    • 2023-02-02
    • 2021-07-27
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多