使用Python从Microsoft Teams抓取文件

时间:2019-07-18 22:58:33

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

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

a。使用Python将Microsoft团队直接从内存拉入内存以处理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/似乎可以让您向团队发送消息,仅此而已?除非我误会了。

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'

1 个答案:

答案 0 :(得分:2)

我设法使用链接的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())

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