简体   繁体   中英

Recieving 'ShareplumRequestError: Sharepoint HTTP Post/Get Failed' When Attempting to Upload Files to Sharepoint

I've been trying to set up a python script that will allow me to automate the uploading of files to Sharepoint, using Shareplum. I eventually managed to bypass the 2FA by setting up an app password, and now if I create a shareplum Folder object using a path to a folder that doesn't exist, the folder is then created on sharepoint - indicating that I have gained some amount of access.

I unfortunately am still yet to manage to either download or upload a file to Sharepoint, as every time I try this, I receive 'ShareplumRequestError: Shareplum HTTP Post/Get Failed: 400 Client Error: Bad Request for url'. In addition, if I try to print the 'files' or 'items' attributes of the folder object, I receive the same error. From my understanding, this error code means that there's something wrong with my request, however I haven't been able to figure out what the issue could be after several hours of fiddling around and looking at various discussions and examples.

Here's the code I've been using:

import shareplum as sp
from credentials import * # import login details: username, app_password

#full path of folder on sharepoint: "/sites/mysiteKeyDocuments/Shared Documents/Test Documents/test"

start_path="/sites/mysiteKeyDocuments"
end_path="Shared Documents/Test Documents/test"
site_url="https://mysite.sharepoint.com"
full_url=site_url+start_path

filename="uploadtest.xlsx"
# A locally stored file, to be uploaded

with open(filename, mode='rb') as file:fileContent = file.read()


version=sp.site.Version.v365 
#version=sp.site.Version.v2016  # Pretty sure it's 365, but have tried both


authcookie = sp.Office365(site_url, username=username, password=app_password).GetCookies()

site = sp.Site(full_url, version=version, authcookie=authcookie)

folder=site.Folder(end_path)
# create Folder object, if folder doesn't exist it creates a new one on sharepoint

### Errors from any line past here


folder.upload_file(fileContent,"new.xlsx")

folder.get_file("dl_test.docx")

folder.upload_file(content=filename,file_name="new.xlsx") # I think this wouldn't work anyway
print(folder.files) 
print(folder.items) 

Running any line beyond the dividing point results in a 400 error of the form shown below, with varying urls depending on the type of request, and with 'HTTP Post Failed' for "upload_file()" and 'HTTP Get Failed' for "get_file()", ".files", and ".items".

Here's an example of the error message output:

Traceback (most recent call last):
  File "S:\Programs\Python386\lib\site-packages\shareplum\request_helper.py", line 17, in post
    response.raise_for_status()
  File "S:\Programs\Python386\lib\site-packages\requests\models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://mysite.sharepoint.com/sites/mysiteKeyDocuments/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/Test%20Documents/test')/Files/add(url='new.xlsx',overwrite=true)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "sharepoint_tests.py", line 27, in <module>
    folder.upload_file(fileContent,"new.xlsx")
  File "S:\Programs\Python386\lib\site-packages\shareplum\folder.py", line 80, in upload_file
    post(self._session, url=url, headers=headers, data=content, timeout=self.timeout)
  File "S:\Programs\Python386\lib\site-packages\shareplum\request_helper.py", line 20, in post
    raise ShareplumRequestError("Shareplum HTTP Post Failed", err)
shareplum.errors.ShareplumRequestError: Shareplum HTTP Post Failed : 400 Client Error: Bad Request for url: https://mysite.sharepoint.com/sites/mysiteKeyDocuments/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/Test%20Documents/test')/Files/add(url='new.xlsx',overwrite=true)

I tested your code in my SPO environment, it works fine when it uploads a small file. If the file size is over 250m, it will prompt the above error:

在此处输入图像描述

You may use another library that offers a way to upload large files:

More reference:

BR

As a temporary workaround you can patch the file YourPackage\dir\site-packages\shareplum\folder.py and on line 10 set self.timeout to a higher value (eg 60).

There is a related Github issue , but the shareplum project seems not to be maintained.

A long term solution would then indeed be better to move to a maintained library as proposed in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM