Forum Discussion
tienit
Aug 08, 2024Copper Contributor
How to generate a direct download link from the shared OneDrive link
Dear Experts and Supporters
I'm a developer working on an issue related to downloading files from OneDrive.
Everything was running fine until a few days ago when I reopened my app
Here are my lines
// shared OneDrive link example
// https://1drv.ms/u/s!Atj71Lw5QEdsrQnTRHMj-fjGc49N?e=hOB5gO
if (string.IsNullOrWhiteSpace(sharingURL))
return string.Empty;
var base64Value = Convert.ToBase64String(Encoding.UTF8.GetBytes(sharingURL));
var encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');
var directlyUrl = $"https://api.onedrive.com/v1.0/shares/{encodedUrl}/root/content";
And now when I cannot download from OneDrive, I have debugged and realized that there was a 401 Unauthorized exception
But the file has been shared for Everyone with Edit permissions.
I thought Microsoft had changed something about the shared link recently and the current way to get the direct download link as it is now is not possible.
The shared link I see it's longer than before and different in format (no s!, no ?e=)
So please kindly help me to solve this case.
Thanks in advance!
Henry Tien (email address removed for privacy reasons)
- hlindqviCopper Contributor
Indeed, the documented way of encoding of "sharing URL" no longer due to authentication failure: https://learn.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0&tabs=csharp#encoding-sharing-urls
I just started to integrate OneDrive so i can't tell if it ever did, but it seems to have worked until recently:
- https://learn.microsoft.com/en-us/answers/questions/2130053/issues-with-onedrive-direct-link-conversion
- https://github.com/felixrieseberg/onedrive-link/issues/1
- https://www.reddit.com/r/PowerShell/comments/10iyowu/onedrive_creating_a_permanent_download_url_to_a/
- https://stackoverflow.com/questions/76464263/how-to-transform-a-onedrive-download-link-to-a-direct-link-that-does-not-prompt
- https://learn.microsoft.com/en-us/answers/questions/1857874/how-to-generate-a-direct-download-link-from-the-sh
- https://stackoverflow.com/questions/79081685/onedrive-auto-download-link
The proposed workaround using an resid query parameter, i.e. driveItem.id, given in an prior reply, is nonsense.
Can anyone from MS comment on this issue, is there an alternative?
- sasiCopper Contributor
Any solid answer available for this question now?
- researchonCyberCopper ContributorHi
This error show because Microsoft has recently changed the format of shared OneDrive links, which has caused your code to stop working and generate a 401 Unauthorized exception. The shared link you provided in the example is in a different format than what your code is expecting. To resolve this issue, you need to update your code to handle the new link format. Here's how you can modify your code to generate the direct download link:
try this
csharp
// shared OneDrive link example
// https://onedrive.live.com/?cid=XXXXXXX&id=XXXXXXX&resid=XXXXXXX
if (string.IsNullOrWhiteSpace(sharingURL))
return string.Empty;
var uri = new Uri(sharingURL);
var queryParams = HttpUtility.ParseQueryString(uri.Query);
var directlyUrl = $"https://api.onedrive.com/v1.0/shares/u!{Uri.EscapeDataString(queryParams["resid"])}/root/content";
Here's what the updated code does:
It parses the shared link using Uri and HttpUtility.ParseQueryString to extract the necessary query parameters.It constructs the direct download link by using the resid query parameter and the api.onedrive.com endpoint. The resid parameter is URL-encoded using Uri.EscapeDataString.
The resulting directlyUrl will look like this:https://api.onedrive.com/v1.0/shares/u!XXXXXXX/root/content
Note -: Make sure to replace the sharingURL variable with the actual shared link you're using- tienitCopper Contributor
Thank you very much for your support and sharing.
I understood and realized that I needed to do 2 things below:
1. change the share link
2. update the source code to handle the new link format
about item number 2, your instructions are very detailed, thanks a lot
about item number 1, currently, on my side, it's like this:
- the link after sharing setup on OneDrive is like below:
https://1drv.ms/u/c/xxxxxxxxxxxxxxxxxxxxxx/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- then I pasted the above link into Chrome incognito mode to see how the link is returned, the result is as below:
From this result, I have seen the 'cid' and 'id', but I can't find the 'resid' anywhere.
Please tell me how to get the 'resid' information then I can create the link as you shared above below:
https://onedrive.live.com/?cid=XXXXXXX&id=XXXXXXX&resid=XXXXXXX
Best regards,
Henry Tien
- tienitCopper Contributor
Hi,
I added the information I did.
I found the 'resid' !
Step 1:
Copied the OneDrive shared link (everyone can edit) below:
https://1drv.ms/u/c/XXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Step 2:
Pasted it on Chrome Incognito, very quickly I caught the link being converted by Microsoft as below (1):
https://onedrive.live.com/?id=XXXXXXXXXXXXXXX%215818&resid=XXXXXXXXXXXXXXX%215818&cid=XXXXXXXXXXXXXXX&redeem=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&migratedtospo=true&v=validatepermission
And finally it's formatted as below (2):
https://onedrive.live.com/?redeem=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&cid=XXXXXXXXXXXXXXX&id=XXXXXXXXXXXXXXX%215818&parId=XXXXXXXXXXXXXXX%214001&o=OneUp
( compared this and found (1).resid = (2).id )
Step 3:
As your instruction, I have created the link below:
https://onedrive.live.com/?cid=XXXXXXXXXXXXXXX&id=XXXXXXXXXXXXXXX%215818&resid=XXXXXXXXXXXXXXX%215818
Step 4:
When above link went through the source code we built, the final direct link we get is:
https://api.onedrive.com/v1.0/shares/u!XXXXXXXXXXXXXXX%215818/root/content
Step 5:
Downloading by source code, and when debugging, instead of the 401 exception as before, it's now a 400 Exception (Bad Request).
Please see the image below
Best Regards,
Henry Tien