Microsoft OneDrive access from UWP app

I’m writing an app that uses OneDrive to share it’s data but I found the documentation a bit jumbled and out of date so I’ll try to post a clear and simple method for connecting to OneDrive from a Windows 10 Universal App written in C#

The first step is to install the Microsoft.OneDriveSDK NuGet package https://github.com/OneDrive/onedrive-sdk-csharp

Then create an App that is registered in Windows Dev Center

You don’t need the ClientID to be stored in the UWP app, the store association takes care of all of that for you. You also don’t need a return URL anymore.

Then to code it

using Microsoft.OneDrive.Sdk;

namespace OneDriveTest

{

public sealed partial class MainPage : Page
{
    private readonly string[] scopes = new string[] { "onedrive.readwrite", "wl.offline_access", "wl.signin" };
    private IOneDriveClient oneDriveClient;
    public MainPage()
    {
        this.InitializeComponent();
        SignIntoOneDrive();
    }

    private async System.Threading.Tasks.Task SignIntoOneDrive()
    {
        if (oneDriveClient == null)
        {
            oneDriveClient = OneDriveClientExtensions.GetUniversalClient(scopes);
            await oneDriveClient.AuthenticateAsync();
        }

    }

}

}

Add a Comment

Your email address will not be published. Required fields are marked *