Skip to content

KoenZomers/OneDriveAPI

Repository files navigation

OneDriveAPI

OneDrive API logo

OneDrive API in .NET Standard 2.0 and .NET 8.0

PRs Welcome

Easy to use lightweight framework to communicate with OneDrive Personal, OneDrive for Business and Microsoft 365 through the Microsoft Graph API (graph.microsoft.com). Authentication is handled through the Microsoft Authentication Library (MSAL.NET), giving you the flexibility to use any Microsoft Entra ID authentication flow (interactive, device code, authorization code, silent, refresh token migration, client credentials).

⚠️ Version 3.0.0.0 is a major, breaking release. Authentication has been migrated to MSAL. OneDriveConsumerApi (the legacy Live Connect based OneDrive Personal API) and OneDriveForBusinessO365Api (the legacy SharePoint REST v2.0 endpoint) have both been removed in favor of OneDriveGraphApi, per Microsoft's guidance that all SharePoint/OneDrive REST innovation is now driven through the Microsoft Graph API. See CHANGELOG.md for full details and migration guidance.

The code contains a fully working demo Windows Forms application which shows you exactly how to use all of the functionality exposed in the OneDrive API.

To get an instance to a Personal OneDrive or OneDrive for Business through the Microsoft Graph API using Microsoft Entra ID, simply use:

KoenZomers.OneDrive.Api oneDrive = new OneDriveGraphApi(applicationId);

Authentication

OneDriveGraphApi exposes the underlying MSAL client application so you can use any MSAL-supported authentication flow, not just the ones wrapped by this library:

  • oneDrive.PublicClientApplication - populated when no client secret is configured (e.g. OneDriveGraphApi without a secret). Use this for AcquireTokenInteractive, AcquireTokenByDeviceCode, AcquireTokenSilent, etc.
  • oneDrive.ConfidentialClientApplication - populated when a client secret is configured. Use this for AcquireTokenByAuthorizationCode, AcquireTokenForClient (app-only), AcquireTokenSilent, etc.

After calling any MSAL flow directly, feed the result back into the library so it can be used for subsequent API calls and silent renewal:

var result = await oneDrive.PublicClientApplication.AcquireTokenInteractive(oneDrive.GetDefaultScopes()).ExecuteAsync();
oneDrive.SetAuthenticationResult(result);

For convenience, the library also wraps the most common flows:

// Authorization code flow (confidential client only - a client secret must be configured)
var authenticateUri = await oneDrive.GetAuthenticationUri();
// ... redirect the user to authenticateUri, capture the redirect callback URL ...
var code = oneDrive.GetAuthorizationTokenFromUrl(callbackUrl);
await oneDrive.AuthenticateUsingAuthorizationCode(code);

// Refresh token migration (e.g. migrating a refresh token obtained outside of MSAL)
await oneDrive.AuthenticateUsingRefreshToken("yourrefreshtoken");

Check the DemoApplication to see both patterns in action.

If you want it to work through a HTTPS proxy, simply provide the proxy configuration by setting the ProxyConfiguration property:

oneDrive.ProxyConfiguration = System.Net.WebRequest.DefaultWebProxy;

Once you have an authenticated OneDrive session, you can simply use for example:

  • Getting all files in the root: oneDrive.GetDriveRootChildren();
  • Downloading a file: oneDrive.DownloadItemAndSaveAs("fileOnOneDrive.txt", "c:\temp\file.txt");
  • Uploading a file: oneDrive.UploadFile("c:\temp\file.txt", "fileOnOneDrive.txt");
  • And many more operations...

Let me know in case you run into other things that no longer work because of this update and I'll be happy to look into it.

Available via NuGet

You can also pull this API in as a NuGet package by adding "KoenZomers.OneDrive.Api" or running:

Install-Package KoenZomers.OneDrive.Api

Package statistics: https://www.nuget.org/packages/KoenZomers.OneDrive.Api

Version History

See CHANGELOG.md for the 3.0.0.0 release notes and MSAL migration guidance.

Register your own Client ID

If you wish to use the OneDrive API through the Microsoft Graph API, you need to register your own Client ID. Follow the steps below to do so.

  1. Go to https://entra.microsoft.com and navigate to Applications > App registrations
  2. At the top, click on New registration
  3. Enter any name for the application that you would like
  4. Under Supported account types select Accounts in any organizational directory (Any Microsoft Entra ID directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)
  5. Under Platform configuration select Public client/native (mobile & desktop)
  6. Hit the Register button at the bottom
  7. Click on Add a platform followed by clicking on Mobile and desktop applications
  8. Enter the redirect URI http://localhost (this is what this library uses by default; MSAL automatically picks a free port on this loopback address for its interactive/authorization-code flows) and click on Configure at the bottom 8b. Under Authentication, make sure Allow public client flows is set to Yes (required for the interactive sign-in flow used by the Demo application)
  9. In the left menu bar, click on Overview
  10. Copy the Application (client) ID from the section at the top
  11. Set the ClientId through your code to the application ID retrieved at the previous step. If you want to use the DemoApplication included with this code to test your new application registration, open its App.config file and replace the value for <add key="GraphApiApplicationId" value="5bbbcf45-3ca9-47cf-8c2f-0ecdcf587332"/> with the application ID retrieved at the previous step.
  12. Run the demo application and click on "Authorize"

Please do not reuse the Client ID that is included in this library. It is only for testing purposes and can be removed at any time and should therefore not be used in production.

Feedback

Feedback is very welcome. If you believe something is not working well or could be improved, feel free to drop me an e-mail or create an issue.

Koen Zomers koen@zomers.eu

About

API in .NET Standard 2.0 and .NET 8.0 to communicate with OneDrive Personal and OneDrive for Business

Topics

Resources

License

Stars

111 stars

Watchers

1 watching

Forks

Contributors

Languages