Search code examples
onedrive

Onedrive API read files on given Link


I want to create project with Microsoft Cognitive Services working with Onedrive API.

Scenario is : User will give me an Onedrive link and my API will go files on that folder.

Is it possible?

If so, where can I find more documentation about it?


Solution

  • You'll want to leverage the shares API using an encoded version of the OneDrive Link.

    GET ../v1.0/shares/{sharingTokenOrUrl}

    Where sharingTokenOrUrl in your case is the URL encoded in the following manner:

    1. First, use base64 encode the URL.
    2. Convert the base64 encoded result to unpadded base64url format by removing = characters from the end of the value, replacing / with _ and + with -.)
    3. Append u! to be beginning of the string.

    As an example, to encode a URL in C#:

    string sharingUrl = "https://onedrive.live.com/redir?resid=1231244193912!12&authKey=1201919!12921!1";
    string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
    string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/','_').Replace('+','-');
    

    Check out this documentation for a complete description of the shares endpoint.