Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Getting Forbidden HTTP Error since 2.3.14

Open mark-blackburn-orpyx opened this issue 2 years ago • 0 comments

I'm getting a forbidden exception when uploading a file to our Sharepoint site.

This is a regression since version 2.3.14. It works fine on 2.3.13.

Here is the exception traceback:

Traceback (most recent call last):
  File "C:\Users\MarkBlackburn\AppData\Local\Programs\Python\Python310\lib\site-packages\office365\runtime\client_request.py", line 58, in execute_query
    response.raise_for_status()
  File "C:\Users\MarkBlackburn\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://company.sharepoint.com/_api/Web/RootFolder/Folders/Add('Shared%20Documents')`

Here is how I'm testing:

import os
import sys
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

baseurl = 'https://compayny.sharepoint.com/'
basesite = 'sites/OurSite'
siteurl = baseurl + basesite

file_content = "Hello World\n"
remotepath = os.path.join("Shared Documents", "Folder1", "Folder2", "myfile.txt")
username = "[email protected]"
password = "PASSWORD"

ctx_auth = AuthenticationContext(baseurl)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(siteurl, ctx_auth) # make sure you auth to the siteurl.

dir, name = os.path.split(remotepath)
ctx.web.ensure_folder_path(dir)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()

mark-blackburn-orpyx avatar Apr 22 '23 16:04 mark-blackburn-orpyx