Hello,
I am new to SharePoint, and trying to upload documents to SharePoint document library. Could you please suggest on how to handle uploading documents to SharePoint using Python.
So far, signed up for 1 month trail SharePoint account and had setup few sites. I am trying this on my Windows 10 laptop.
Getting "permission denied" error while running below Python script.
SP-upload_test.py
============
import requests
from requests_ntlm import HttpNtlmAuth
fileName = 'C:\\Users\\xxxx\\Documents\\Vissu.pdf'
#Enter your SharePoint site and target library
sharePointUrl = 'https://xxxx.sharepoint.com'
folderUrl = '/sites/MyTeam/Internal'
#Sets up the url for requesting a file upload
requestUrl = sharePointUrl + '/_api/web/getfolderbyserverrelativeurl(\'' + folderUrl + '\')/Files/add(url=\'' + fileName + '\',overwrite=true)'
#Read in the file that we are going to upload
file = open(fileName, 'rb')
#Setup the required headers for communicating with SharePoint
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose'}
#Execute a request to get the FormDigestValue. This will be used to authenticate our upload request
r = requests.post(sharePointUrl + "/_api/contextinfo",auth=HttpNtlmAuth('domain\\user','password'), headers=headers)
print(r.json())
formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']
#Update headers to use the newly acquired FormDigestValue
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}
#Execute the request. If you run into issues, inspect the contents of uploadResult
uploadResult = requests.post(requestUrl,auth=HttpNtlmAuth('domain//user','password'), headers=headers, data=file.read())
==================================================================================
print(r.json()) prints below info
{'error': {'code': '-2147024891, System.UnauthorizedAccessException', 'message': {'lang': 'en-US', 'value': 'Access denied. You do not have permission to perform this action or access this resource.'}}}
Receiving "Access denied" for both private and public member, part of privacy settings for the site.
Let me know for any other information. Thanks in advance!