KoenZomers / OneDriveAPI

API in .NET Framework 4.8.0, .NET Core 3.1 and .NET 6.0 to communicate with OneDrive Personal and OneDrive for Business
Eclipse Public License 1.0
110 stars 34 forks source link

OneDriveAPI

OneDrive API in .NET Framework 4.8.0, .NET Core 3.1 and .NET 6.0

PRs Welcome

Easy to use lightweight framework to communicate with the OneDrive Personal and OneDrive for Business through either the Azure AD (api.onedrive.com & tenant-my.sharepoint.com/_api or the Azure AD V2.0 (graph.microsoft.com) endpoint. It allows communication through one unified piece of code with:

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 OneDrive or OneDrive for Business through the Microsoft Graph API using Azure AD v2.0 (recommended), simply use:

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

OR to get an instance to a OneDrive Consumer account, simply use:

KoenZomers.OneDrive.Api oneDrive = new OneDriveConsumerApi(clientId, clientSecret);

OR to get an instance to a OneDrive for Business account, simply use:

KoenZomers.OneDrive.Api oneDrive = new OneDriveForBusinessO365Api(clientId, clientSecret);

!! Notice: Microsoft will turn off the Office 365 Discovery API on November 1, 2019 which is being used by the OneDrive for Business Api. Use the OneDriveGraphApi instead to avoid your application to stop working on this date !!

If you're not sure which of these to use, go with the Microsoft Graph using the Azure AD v2.0 endpoint.

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;

In order to get a new access token from the refresh token you already got from authenticating to OneDrive or OneDrive for Business, simply use:

oneDrive.AuthenticateUsingRefreshToken("yourrefreshtoken");

If you don't have a refresh token yet, you will have to go through an interactive browser logon to perform authentication and get the refresh token. Check the DemoApplication to see how this works.

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

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

2.4.0.0 - May 18, 2022

2.3.2.0 - February 2, 2021

2.3.1.1 - November 16, 2020

2.3.1.0 - October 27, 2019

2.3.0.2 - May 16, 2019

2.3.0.1 - May 5, 2019

2.2.1.0 - May 5, 2019

2.2.0.0 - March 3, 2019

2.1.2.1 - February 17, 2019

2.1.1.0 - January 4, 2019

2.1.0.1 - January 12, 2018

2.1.0.0 - January 11, 2018

2.0.4.3 - January 5, 2018

2.0.4.2 - January 5, 2018

2.0.4.1 - January 5, 2018

2.0.4.0 - January 5, 2018

2.0.3.1 - December 31, 2017

2.0.3.0 - October 31, 2017

2.0.2.0 - October 30, 2017

2.0.1.0 - August 23, 2017

2.0.0.0 - August 21, 2017

Version History

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://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
  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 Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)
  5. Under Platform configuration select Client Application
  6. Hit the Register button at the bottom
  7. Click on Add a platform followed by clicking on Mobile and desktop applications
  8. Select the two proposed options for https://login.microsoftonline.com/common/oauth2/nativeclient and _https://login.live.com/oauth20_desktop.srf_ and click on Configure at the bottom
  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, select "Graph API (Consumer & Business)" and click on "Authorize"

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