O365 / python-o365

A simple python library to interact with Microsoft Graph and Office 365 API
Apache License 2.0
1.65k stars 419 forks source link

Client Error: 404 Client Error: Not Found for url: when switching between sharepoint and onedrive resources #1034

Open ysguy opened 9 months ago

ysguy commented 9 months ago

I have two python files, one calls a sharepoint doc library, attaches and reads an excel file. the other does the same for the onedrive associated with the azure application profile. I can read one but the other will get a 404 error. If i wait hours, and try the other with zero code changes it will work, but the other will break. It appears the client is somehow caching the site library URL.

requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/me/drive/items/01SPB5GSZEZAQBYVNWDBAI7JVC3FTW3RKJ/workbook/createSession | Error Message: The resource could not be found.

I've seen others post similar messages that are years old now. It appears that the drive ID is not changing between requests.  

I can see the filename, and the search works and returns the correct file object, but it always dies at od_excel_file = WorkBook(XLfile)

the XLfile reference works because i have code a line before it that prints out the name per your guide.

print(f'OneDrive Searched Files Found: {XLfile}') Which returns 'myfile.xlsx'

Do i need to close session on the workbook object somehow? I see nothing in the docs about this.

ysguy commented 9 months ago

Resolved. Thanks for being my Rubber Duckie. Changing my code to not persist immediately resolves it. Sorry for missing this important information.

The Fix: od_excel_file = WorkBook(XLfile, use_session=False, persist=False)

ysguy commented 9 months ago

Reopening issue. It works randomly, but a majority of the time it still gives the 404 error with the use_session and persist false values.

How to test. Make one python or two python scripts that attach to an excel on onedrive and sharepoint doc libraries. Call one, then the other. The error will appear.

alejcas commented 8 months ago

Can I see the error messages? I don't know what's happening and I don't know If I will be able to fix it

parkus83 commented 8 months ago

Hello everyone, I have the same problem. I am trying to modify an Excel file on SharePoint, and I can access it using ysguy's workaround (od_excel_file = WorkBook(XLfile, use_session=False, persist=False). However, when I try to access the sheets, columns, or individual rows, it gives me this error again:

requests.exceptions.HTTPError: 404 Client Error: Not Found for the URL: https://graph.microsoft.com/v1.0/me/drive/items/01GOIU2PSQMCWJ47LBVRA33LRPXUHQCGDK/workbook/worksheets/MIGRATION Error Message: The resource could not be found.

if I check the link provided above I retrieve a weird token error:

{ error: { code: "InvalidAuthenticationToken", message: "Access token is empty.", innerError: { date: "2024-02-05T10:52:47", request-id: "c989ec4f-1cf9-4c8b-9de0-92692a141602", client-request-id: "c989ec4f-1cf9-4c8b-9de0-92692a141602" } } }

ps: I have used the following method to authenticate my application. And it return the default_storage correctly.

def access_sharepoint_drive() -> Drive:
    """
    Access sharepoint drive
    :return: sharepoint drive
    """
    credentials = (os.environ.get("CLIENT_ID_SHAREPOINT"), os.environ.get("CLIENT_SECRET_SHAREPOINT"))

    account = Account(credentials)

    # used only for the first time
    # scopes = ['basic', 'Files.ReadWrite.All', 'Files.Read.All']
    # if account.authenticate(scopes=scopes):
    #   logging.info("Authenticated to sharepoint")

    storage = account.storage()

    return storage.get_default_drive()
alejcas commented 7 months ago

Hi I don't have the time to investigate this atm.

If you want to, a good start are ms graph docs:

https://learn.microsoft.com/en-us/graph/api/resources/excel?view=graph-rest-1.0