簡體   English   中英

使用Python上傳文件到Sharepoint

[英]To upload files to Sharepoint using Python

我正在嘗試編寫一個腳本,將文件上傳到 Sharepoint。因此,我公司的 Sharepoint 中有幾個文件夾,但我只被授予對一個特定文件夾的訪問權限。 我嘗試使用以下代碼,但出現錯誤:“...訪問已被條件訪問策略阻止。訪問策略不允許頒發令牌。” 我認為我錯誤地指定了 Sharepoint 中的文件夾,因為那里有太多文件夾,鏈接中有 %s 和符號。

在該股東中,我需要在“2023 文件夾”中上傳。 它們是 GlobalDocs -> LocalDocs -> 2023。sharepoint_url 很可能是 'https://company.sharepoint.com/sites/GlobalDocs/' 我不確定,sp_relative_url 應該選擇什么。

整個鏈接充滿了 % 和不同的符號。

https://company.sharepoint.com/sites/GlobalDocs/Local%20Docs/Forms/AllItems.aspx?id=%2Fsites%2Fsmth%2F2Fsmth%20smth%2Fsmth%2Fsmth&viewid=smth

你能幫我嗎,請問如何理解縮短此鏈接以將其放入 sp_relative_url 的方法? 您認為 sp_relative_url 可能是此錯誤的問題,還是我誤會了?

從 office365.runtime.auth.user_credential 導入 UserCredential

從 office365.sharepoint.client_context 導入 ClientContext

def get_sharepoint_context_using_user():

**sharepoint_url** = 'https://company.sharepoint.com/sites/GlobalDocs/'
user_credentials = UserCredential("email@...", "password")
ctx = ClientContext(sharepoint_url).with_credentials(user_credentials)
return ctx

def upload_to_sharepoint(文件名:海峽):

**sp_relative_url**='https://company.sharepoint.com/sites/GlobalDocs/smth with many %' ???
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()

先感謝您!!!

您是否嘗試過使用 %20 而不是空格? 請嘗試這是否適合您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM