通过Python上传文件到SharePoint

问题描述 投票:0回答:1

我正在尝试通过 Python 将文件上传到 SharePoint。 我尝试了以下方法:

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext

def get_sharepoint_context_using_user():
sharepoint_url = 'https://companyname.sharepoint.com/sites/Afolder/'

user_credentials = UserCredential("login", "password")

ctx = ClientContext(sharepoint_url).with_credentials(user_credentials)

return ctx



def upload_to_sharepoint(file_name: str):
sp_relative_url='https://companyname.sharepoint.com/sites/Afolder/some%20thing/Forms/AllItems.aspx'

ctx = get_sharepoint_context_using_user()
ctx = get_sharepoint_context_using_user()
target_folder = ctx.web.get_folder_by_server_relative_url(sp_relative_url)


with open(file_name, 'rb') as content_file:
    file_content = content_file.read()
    target_folder.upload_file(file_name, file_content).execute_query()

我尝试调用上传功能后

upload_to_sharepoint(os.path.join(path_from, file_word))

我犯了以下错误:

ValueError: Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf

也许,你知道这可能是什么原因。我尝试了不同的变体,但没有用。 棘手的部分是我获得的访问权限不是整个文件夹,我的意思是 https://companyname.sharepoint.com,而是从 https://companyname.sharepoint.com/sites 开始的部分/文件夹。 这可能是这个问题的原因吗?如果是,当访问受限时,是否有机会通过 Python 上传到文件夹。

提前谢谢你!

python python-3.x sharepoint upload office365api
1个回答
0
投票

问题是,当无法从指定的 URL 检索身份验证令牌时,此错误更多是身份验证凭据的问题。所以笼统地说,不仅是文件夹没有权限。

© www.soinside.com 2019 - 2024. All rights reserved.