I'm trying to download a file from sharepoint using my Flutter App.
I'm doing a GET request at this url: https://mytenant.sharepoint.com/sites/siteName/Lists/Disposizioni%20di%20servizio/Attachments/2/tper_Bo053.pdf
but I always get a 401.
I tried with the Graph API token and with the Sharepoint API one, but same result.
These are the scopes I'm using:
Graph API -> "openid profile offline_access Sites.Read.All Files.Read.All"
Sharepoint API -> "https://myTenant.sharepoint.com/.default"
Also, these are the permissions on azure:
Am I missing something? This PDF is an attachment on a list item of the site, it's not in the documents folder of the site. I'm able to do all the API request to get the items and their attachments, but then when I request the pdf url I get 401. Any suggestion?
Yes you are missing how sharepoint authenticates users. You cannot download files like this using that token (SP uses cookies for user auth). Means, SharePoint uses different authentication schemals for users and applications. Your URL is not an API call, it is just a normal "user" URL. You should use API instead (below is REST, but you can also use graph):
path:
"/sites/siteName/Lists/Disposizioni%20di%20servizio/Attachments/2/tper_Bo053.pdf"
Download usign REST API:
https://mytenant.sharepoint.com/sites/siteName/_api/web/GetFileByServerRelativeUrl('{path}')/$value
Note that if you just need to download a file, you can use Microsoft CLI (command line interface) for that, it has commands to easily download/upload files, you don't need to build any application for that, and you can easily use that from scripts like python scripts for example.