[Python] OneDrive Picture Download using python msal fails with 401 Error "unauthenticated"

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to download my (personal) photos from OneDrive (want to backup them on my NAS)

    Created an API Key in Azure Portal. Enterprise applications | All applications

    Added "http://localhost" to redirect URL. Using a "Desktop Application" (so no client_secret allowed).

    Added the following Rights to the API Key: application rights

    User.Read
    User.Export.All
    Files.ReadWrite.All



    Using (python) MSAL library:

    $ pip list | grep msal
    msal 1.32.0
    msal-extensions 1.3.1



    Using PublicClientApplication and "consumers" endpoint to create an app:

    app = PublicClientApplication(
    app_id,
    authority="https://login.microsoftonline.com/consumers")


    Browser opens, I confirm, browser gets redirected to localhost:


    Authentication completed. You can close this window now.
    ​

    Creating a token with the following scopes:

    scopes = ["User.Read", "User.Export.All", "Files.ReadWrite.All"]
    result = app.acquire_token_interactive(scopes=scopes)


    Token gets returned.

    Using that token to Download Infos for the Files stored: request_url = 'https://graph.microsoft.com/v1.0/me/drive/root:/Bilder/Eigene Aufnahmen:/children'

    That works fine and returns json information for all files in this folder, e.g.: "@microsoft.graph.downloadUrl": "https://my.microsoftpersonalcontent.com/personal/bfc1c0d9bbadf7cd/_layouts/15/download.aspx?UniqueId=bbadf7cd-c0d9-20c1-80bf-042d00000000&Translate=false&tempauth=v1e.eyJzaXRlaWQiOiI0OTgwMzNhOC00MWVjLTQ4Y2QtYTg2YS0xYjYwNjViMmUyMDUiLCJhcHBfZGlzcGxheW5hbWUiOiJQZXJzb25hbCBadWdyaWZmIGF1ZiBPbmVEcml2ZSIsImFwcGlkIjoiNmNhZTJhMmEtNzAwOS00ZDY2LWE5MjgtYmU1ZjQ[...]

    When using this URL to download the Photo, i get:

    {"error":{"code":"unauthenticated","message":"Unauthenticated"}}
    401



    HTTP error 401 and "unauthenticated" message ..

    Can someone point me to a direction?

    If retrieving information about the folders using the access token works fine, why does it return 401 when trying to download the content of the file?

    Continue reading...

Compartilhe esta Página