How to generate a direct download link from the shared OneDrive link

Nguyen Tien 1 Reputation point
2024-08-08T14:17:54.6433333+00:00

Dear Experts and Supporters,

I'm a developer working on an issue related to downloading files from OneDrive. Everything was running fine since 2021 until a few days ago when I reopened my app

Here is 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";

Note: I have also referenced from many sources and how to encode a shared link as below: Encoding sharing URLs

And now when I cannot download from OneDrive, I have debugged and realized that there was a 401 Unauthorized exception

But the files have been shared for Everyone with Edit permissions.

I thought Microsoft has changed something about the shared link recently and the current way to get the directly 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 (******@outlook.com)

Microsoft 365 and Office | OneDrive | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiajing Hua 18,065 Reputation points Moderator
    2024-08-09T05:27:18.8533333+00:00

    Hi @Nguyen Tien

    As your issue is related to the development of OneDrive, but we deal with general issues of OneDrive, it's recommended to post a new thread in Microsoft Tech Community --- OneDrive Developer: https://techcommunity.microsoft.com/t5/onedrive-developer/bd-p/OneDriveDeveloper

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.