首页 > 解决方案 > 如何使用 python 打开浏览器并连接到共享点?

问题描述

我需要将文件从本地 PC 上传到共享点,但对于我的帐户,我启用了 2 因素。如何绕过或使用python进行身份验证并将文件上传到其中?

我尝试了下面的代码,但我不确定它是否正确!

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
import sharepointparameters as parameters
baseurl = 'https://dxcportal.sharepoint.com'
basesite = '/_layouts/15/sharepoint.aspx' # every share point has a home.
siteurl = baseurl + basesite

localpath = "demandsheet.xlsx"
remotepath = "datafiles/demandsheet.xlsx" # existing folder path under sharepoint site.

ctx_auth = AuthenticationContext(siteurl)
ctx_auth.acquire_token_for_user(parameters.username, parameters.password)
ctx = ClientContext(siteurl, ctx_auth) # make sure you auth to the siteurl.with open(localpath, 'rb') as content_file:
file_content = content_file.read()

dir, name = os.path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()

标签: python-3.xfile-uploadsharepoint-online

解决方案


推荐阅读