Running the below code, I receive a 403 forbidden error. Far below is the error. Was wondering why I would be receiving this error. Any help would be greatly appreciated.
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
# def get_sharepoint_context_using_user():
# Get sharepoint credentials
# sharepoint_url = 'https://comcastcorp.sharepoint.com'
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
# def get_sharepoint_context_using_user():
# Get sharepoint credentials
# sharepoint_url = 'https://comcastcorp.sharepoint.com'
# # Initialize the client credentials
# user_credentials = UserCredential("abc", "123")
# # create client context object
# ctx = ClientContext(sharepoint_url).with_credentials(user_credentials)
# web = ctx.web
# ctx.load(web)
# ctx.execute_query()
# return ctx
# get_sharepoint_context_using_user
#Constructing Details For Authenticating SharePoint
client_id = "abc"
client_secret = "123"
site_url = "https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/_api/site/id"
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
creds = ClientCredential(client_id, client_secret)
ctx = ClientContext(site_url).with_credentials(creds)
web = ctx.web
ctx.load(web)
ctx.execute_query()
Here is the error:
C:\Work\Programming\sharepointDrive\python>python sharePointUploadTestNewest.py
Traceback (most recent call last):
File “C:\Users\pMalle001\AppData\Local\Programs\Python\Python311\Lib\site-packages\office365\runtime\client_request.py”, line 58, in execute_query
response.raise_for_status()
File “C:\Users\pMalle001\AppData\Local\Programs\Python\Python311\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://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/_api/site/id/_api/Web
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Work\Programming\sharepointDrive\python\sharePointUploadTestNewest.py”, line 47, in
ctx.execute_query()
File “C:\Users\pMalle001\AppData\Local\Programs\Python\Python311\Lib\site-packages\office365\runtime\client_runtime_context.py”, line 187, in execute_query
self.pending_request().execute_query(qry)
File “C:\Users\pMalle001\AppData\Local\Programs\Python\Python311\Lib\site-packages\office365\runtime\client_request.py”, line 62, in execute_query
raise ClientRequestException(*e.args, response=e.response)
office365.runtime.client_request_exception.ClientRequestException: (‘-2147024891, System.UnauthorizedAccessException’, ‘Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))’, ‘403 Client Error: Forbidden for url: https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/_api/site/id/_api/Web’)