An API that connects multiple Microsoft services, enabling data access and automation across platforms
Accessing publicly shared files/folders via Graph API
Hello,
I'm developing a program that should access the files that are shared "to anyone with the link". I need to do it with my developer account so the user won't be prompted to login (I use application permissions), however after many tries I can't find the proper way to do it.
Currently I am using a free test developer tenant (further called developer account) which will be used by my app and my private account, from which I want to get files/folders.
What I want to achieve is to be able to access the folder and its contents that was shared by anyone from his/her personal OneDrive in my app, using the credentials created in developer account. I've created App Registration, created Client Secret, added Application Permissions (Files.ReadWrite.All) and am able to get the token correctly via API:
(POST) https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
using correct
"tenant",
"client_id",
and "client_secret" parameters,
"scope": "https://graph.microsoft.com/.default"
and "grant_type": "client_credentials"
Next I get the shareable link to the folder from my personal OneDrive and I encode it as instructed here.
However when I'm trying to access it via API I get the 403 Forbidden status and the message:
(GET) https://graph.microsoft.com/v1.0/shares/<correctlyEncodedURL>
{
"error": {
"code": "accessDenied",
"message": "The sharing link no longer exists, or you do not have permission to access it.",
"innerError": {
"date": "2022-06-01T01:21:40",
"request-id": "1ba25cb7-6557-42ca-b046-e63a9dff65ae",
"client-request-id": "1ba25cb7-6557-42ca-b046-e63a9dff65ae"
}
}
}
I know that the Bearer Token is correct, because the /shares/ request works properly when accessing files or folders created in the developer OneDrive, inside the test tenant. I want however to be able to access ALL publicly accessible files...
Furthermore, during my investigation I was also trying to set it from the API context, thinking that maybe the problem is somehow incorrectly shared file, so I used the Graph Explorer as below:
(POST) https://graph.microsoft.com/v1.0/drives/<driveID>/items/<itemID>/createLink
{
"type": "view",
"scope": "anonymous"
}
using credentials from the private account. It works, I get the ShareId value and also a WebUrl value.
Using Graph Explorer, I can access the folder using (GET) https://graph.microsoft.com/v1.0/shares/<ShareID> or https://graph.microsoft.com/v1.0/shares/<encodedWebURL> but ONLY when signed in with my private account. I get proper id, @odata.context etc.
When using developer account the result is the same as above - "The sharing link no longer exists, or you do not have permission to access it."
I will really appreciate if you help me find the solution to my problem.
Thanks,
Mateusz