gpt4 book ai didi

python-3.x - 使用 Python 从 Microsoft Teams 抓取文件

转载 作者:行者123 更新时间:2023-12-03 23:11:05 57 4
gpt4 key购买 nike

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

一种。直接从微软团队拉入内存使用 Python 与 Pandas 一起处理

湾将团队中的文件复制到共享网络文件夹中(然后 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'

最佳答案

我已设法使用您链接的 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 等中进行处理

关于python-3.x - 使用 Python 从 Microsoft Teams 抓取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57103597/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com