我正在使用下面的代码连接到sharepoint,目的是将文件上传到网站。所有的auth东西都很好,由于某种原因,我得到了一个json解码错误。这里有什么明显的吗?假设流从函数“getSharepointContext()”开始。我已经通过了许多其他教程和问题,关于这一点,一切似乎都很好,一致。
目的是循环访问本地文件夹中的每个文件并上载到sharepoint站点/文件夹。
很明显,请求和json.loads有问题,但是如果有更多处理这样的文件的经验的人看到了一些东西,我会很感激的!
import os
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
from logger import Logger
def getSharepointContext():
sharepointUrl = 'https://url.sharepoint.com/site/folder/'
clientCredentials = ClientCredential('a79f867e-3138-4qwdqwdqwdqwdqwdffe3', '_vp8Q~05OqwdqwdqwdqwdyCb_gQppjen8czP')
ctx = ClientContext(sharepointUrl).with_credentials(clientCredentials)
targetFolder = ctx.web.get_folder_by_server_relative_url(sharepointUrl)
uploadFiles(targetFolder)
def uploadFiles(targetFolder):
wd = os.getcwd()
td = wd+'/toUpload'
for fn in os.listdir(td):
f = os.path.join(td, fn)
if os.path.isfile(f):
with open(f, 'rb') as k:
Logger.log('Uploading: ' + f, 1, True)
file_content = k.read()
print(file_content)
targetFolder.upload_file(f, file_content).execute_query()
if __name__ == "__main__":
getSharepointContext()
输出:
Traceback (most recent call last):
File "C:\Users\mnowicky\AppData\Roaming\Python\Python311\site-packages\requests\models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\projects\odUploader_py\uploadFiles.py", line 46, in <module>
getSharepointContext()
File "D:\projects\odUploader_py\uploadFiles.py", line 17, in getSharepointContext
uploadFiles(targetFolder)
File "D:\projects\odUploader_py\uploadFiles.py", line 31, in uploadFiles
targetFolder.upload_file(f, file_content).execute_query()
File " \client_object.py", line 44, in execute_query
self.context.execute_query()
File " \client_runtime_context.py", line 161, in execute_query
self.pending_request().execute_query(qry)
File " \client_request.py", line 57, in execute_query
response = self.execute_request_direct(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File " \client_request.py", line 69, in execute_request_direct
self.beforeExecute.notify(request)
File " \types\event_handler.py", line 21, in notify
listener(*args, **kwargs)
File , line 221, in _build_modification_query
self._ensure_form_digest(request)
File , line 157, in _ensure_form_digest
self._ctx_web_info = self._get_context_web_information()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File , line 170, in _get_context_web_information
client.map_json(response.json(), return_value, json_format)
^^^^^^^^^^^^^^^
File "C:models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
1条答案
按热度按时间zfciruhq1#
更方便的方法是使用Microsoft Graph API,它提供了一个专用的上传API端点,您可以参考以下文档:
https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
小尺寸上传:
PUT /drives/{drive-id}/items/{parent-id}:/{filename}:/content
可恢复上传:
POST /drives/{driveId}/items/{itemId}/createUploadSession
PUT https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337
Content-Length: 26
Content-Range: bytes 0-25/128