Forum Discussion
How to generate a direct download link from the shared OneDrive link
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
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:
https://onedrive.live.com/?redeem=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&cid=XXXXXXXXXXXXXXXX&id=XXXXXXXXXXXXXXXX%215818&parId=XXXXXXXXXXXXXXXX%214001&o=OneUp
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
- tienitAug 09, 2024Copper 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
- tienitAug 10, 2024Copper Contributor
- rubendescensosSep 13, 2024Copper Contributor
tengo el mismo problema si encue tra alguien solucion que informe porfavor tienit