vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.34k stars 336 forks source link

404 when trying to read a file from sharepoint #583

Open andreas789 opened 2 years ago

andreas789 commented 2 years ago

I am trying to read some files from a SharePoint 365 location using the office-365 python rest API. I created an app principal and used it to get the files.

I am able to list the directory using the code below, so I have no problem authenticating.

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.files.file import File
import io
import pandas as pd

site_url = 'https://*.sharepoint.com/sites/sharepoint_to_dbfs/'

app_principal = {
'client_id': '*',
'client_secret': '*',
}

ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret']):
    ctx = ClientContext(site_url, ctx_auth)
    web = ctx.web
    ctx.load(web)
    ctx.execute_query()
    print('Authenticated into sharepoint app for: ',web.properties['Title'])
    lists = ctx.web.lists
    ctx.load(lists)
    ctx.execute_query()
    for l in lists:
        print("This is a list object: {0}".format(l.properties['Title']))
    list_object = ctx.web.lists.get_by_title('Documents')
    items = list_object.items
    ctx.load(items)
    ctx.execute_query()
    for l in items:
        print("This is a item object: {0}".format(l.properties['Title']))

else:
    print(ctx_auth.get_last_error())

However, when trying to get an excel file loaded onto a pandas df I am getting an error.

file_url = "/sites/sharepoint_to_dbfs/Test/Book.xlsx"

response= File.open_binary(ctx, file_url)
    # save data to BytesIO stream
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0)  # set file object to start
    # load Excel file from BytesIO stream
df = pd.read_excel(bytes_file_obj, engine='openpyxl')

The error is:

File is not a zip file.

When checking the response I got, I see a 404 error and the following content. The file however exists on that path.

b'{"error":{"code":"-2130575338, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The file /sites/sharepoint_to_dbfs/Test/Book.xlsx does not exist."}}}'
andreas789 commented 2 years ago

Any clues on how to solve that?

Zangetsu89 commented 1 year ago

same issue