Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit bd4c8a1

Browse files
committed
2 parents c410156 + 6e4d32b commit bd4c8a1

File tree

4 files changed

+36
-58
lines changed

4 files changed

+36
-58
lines changed

Api/API.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
<AssemblyName>KoenZomers.OneDrive.Api</AssemblyName>
66
<SignAssembly>true</SignAssembly>
77
<AssemblyOriginatorKeyFile>KoenZomers.OneDrive.Api.snk</AssemblyOriginatorKeyFile>
8-
<Version>2.3.0.2</Version>
8+
<Version>2.3.1.0</Version>
99
<Authors>Koen Zomers</Authors>
1010
<Company>Koen Zomers</Company>
1111
<Description>API in .NET Standard 2.0, .NET Framework 4.5.2, .NET Framework 4.7.2 and .NET Core 2.0 to communicate with OneDrive Personal and OneDrive for Business</Description>
1212
<PackageProjectUrl>https://github.com/KoenZomers/OneDriveAPI</PackageProjectUrl>
1313
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
14-
<PackageReleaseNotes>- Fixed a bug when uploading files larger than 5 MB throwing an exception stating that the oneDriveUploadSession is NULL</PackageReleaseNotes>
14+
<PackageReleaseNotes>- Merged [PR 20](https://github.com/KoenZomers/OneDriveAPI/pull/20) to allow for providing a client secret with the OneDrive Graph API</PackageReleaseNotes>
1515
<PackageLicenseUrl>https://github.com/KoenZomers/OneDriveAPI/blob/master/LICENSE.md</PackageLicenseUrl>
1616
<Copyright>Koen Zomers</Copyright>
1717
<RootNamespace>KoenZomers.OneDrive.Api</RootNamespace>
18+
<AssemblyVersion>2.3.1.0</AssemblyVersion>
19+
<FileVersion>2.3.1.0</FileVersion>
1820
</PropertyGroup>
1921

2022
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">

Api/KoenZomers.OneDrive.Api.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Api/OneDriveGraphApi.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public class OneDriveGraphApi : OneDriveApi
6767
/// Instantiates a new instance of the Graph API
6868
/// </summary>
6969
/// <param name="applicationId">Microsoft Application ID to use to connect</param>
70-
public OneDriveGraphApi(string applicationId) : base(applicationId, null)
70+
/// <param name="clientSecret">Microsoft Application secret to use to connect</param>
71+
public OneDriveGraphApi(string applicationId, string clientSecret = null) : base(applicationId, clientSecret)
7172
{
7273
OneDriveApiBaseUrl = GraphApiBaseUrl + "me/";
7374
}
@@ -112,14 +113,15 @@ protected async Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(s
112113
queryBuilder.Add("code", authorizationToken);
113114
queryBuilder.Add("redirect_uri", AuthenticationRedirectUrl);
114115
queryBuilder.Add("grant_type", "authorization_code");
116+
if (ClientSecret != null)
117+
queryBuilder.Add("client_secret", ClientSecret);
115118
return await PostToTokenEndPoint(queryBuilder);
116119
}
117120

118121
/// <summary>
119122
/// Gets an access token from the provided refresh token using the default scopes defined in DefaultScopes
120123
/// </summary>
121124
/// <param name="refreshToken">Refresh token</param>
122-
/// <param name="scopes">Scopes to request access for</param>
123125
/// <returns>Access token for the Graph API</returns>
124126
/// <exception cref="Exceptions.TokenRetrievalFailedException">Thrown when unable to retrieve a valid access token</exception>
125127
protected override async Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken)
@@ -142,6 +144,8 @@ protected async Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string
142144
queryBuilder.Add("refresh_token", refreshToken);
143145
queryBuilder.Add("redirect_uri", AuthenticationRedirectUrl);
144146
queryBuilder.Add("grant_type", "refresh_token");
147+
if (ClientSecret != null)
148+
queryBuilder.Add("client_secret", ClientSecret);
145149
return await PostToTokenEndPoint(queryBuilder);
146150
}
147151

@@ -452,7 +456,7 @@ public async Task<OneDriveCollectionResponse<OneDrivePermission>> ListPermission
452456
/// <summary>
453457
/// Lists all permissions on a OneDrive item
454458
/// </summary>
455-
/// <param name="itemPath">The OneDrive item to retrieve the permissions of</param>
459+
/// <param name="item">The OneDrive item to retrieve the permissions of</param>
456460
/// <returns>Collection with OneDrivePermission objects which indicate the permissions on the item</returns>
457461
public async Task<OneDriveCollectionResponse<OneDrivePermission>> ListPermissions(OneDriveItem item)
458462
{
@@ -1023,6 +1027,7 @@ protected async Task<OneDriveUploadSession> CreateResumableUploadSessionInternal
10231027
/// <summary>
10241028
/// Uploads a file to OneDrive using the resumable file upload method
10251029
/// </summary>
1030+
/// <param name="fileStream">Stream with the content to upload</param>
10261031
/// <param name="oneDriveUploadSession">Upload session under which the upload will be performed</param>
10271032
/// <param name="fragmentSizeInBytes">Size in bytes of the fragments to use for uploading. Higher numbers are faster but require more stable connections, lower numbers are slower but work better with unstable connections.</param>
10281033
/// <returns>OneDriveItem instance representing the uploaded item</returns>
@@ -1161,7 +1166,6 @@ public virtual async Task<SharePointSite> GetSiteByPath(string hostname, string
11611166
/// <summary>
11621167
/// Gets a SharePoint site belonging to a group
11631168
/// </summary>
1164-
/// <param name="hostname"></param>
11651169
/// <param name="groupId">Unique identifier of group to retrieve the associated SharePoint site for</param>
11661170
/// <returns>SharePointSite instance containing the details of the requested site in SharePoint</returns>
11671171
public virtual async Task<SharePointSite> GetSiteByGroupId(string groupId)

README.md

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# OneDriveAPI
22
OneDrive API in .NET Standard 2.0, .NET Framework 4.5.2, .NET Framework 4.7.2 and .NET Core 2.0
33

4+
![](https://img.shields.io/github/issues/koenzomers/OneDriveAPI.svg) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
5+
46
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:
57

68
- OneDrive Personal
@@ -67,6 +69,10 @@ https://www.nuget.org/packages/KoenZomers.OneDrive.Api
6769

6870
## Version History
6971

72+
2.3.1.0 - October 27, 2019
73+
74+
- Merged [PR 20](https://github.com/KoenZomers/OneDriveAPI/pull/20) to allow for providing a client secret with the OneDrive Graph API
75+
7076
2.3.0.2 - May 16, 2019
7177

7278
- Fixed a bug when uploading files larger than 5 MB throwing an exception stating that the oneDriveUploadSession is NULL
@@ -153,57 +159,22 @@ https://www.nuget.org/packages/KoenZomers.OneDrive.Api
153159

154160
[Version History](./VersionHistory.md)
155161

156-
## Register your own Client ID / Client Secret
157-
158-
If you wish to use the OneDrive API, you need to register your own Client ID / Client Secret. Depending on whether you want to target OneDrive for Consumers or OneDrive for Business, follow the steps below to do so.
159-
160-
### OneDrive via Azure AD v2.0 / Microsoft Graph (recommended)
161-
162-
1. Go to https://apps.dev.microsoft.com
163-
2. Log in with your Microsoft or school or work account
164-
3. Click on "Add an app" next to "Converged applications"
165-
4. Give it any name you'd like and uncheck the "Let us help you get started" box
166-
5. Click Create
167-
6. Copy the Application Id shown on the screen and use it in this app for the GraphApiApplicationId App.config AppSetting. Make sure you set the redirect URL to https://login.microsoftonline.com/common/oauth2/nativeclient. Allow implicit flow can stay unchecked, logout URL can be left empty, Microsoft Graph Permissions not need to be specified here.
168-
169-
### OneDrive for Consumers via Azure AD / api.onedrive.com
170-
171-
** NOTE: Microsoft updated the web interface, so the steps below have been updated to reflect this (August 9, 2016) **
172-
173-
1. Go to https://account.live.com/developers/applications/index
174-
2. Log in with your Microsoft Account
175-
3. Click on "Add an app" next to the "My applications" section
176-
4. Give it any name you would like and click on "Create application"
177-
5. It will show you the Application Id which you have copy to the App.config OneDriveConsumerApiClientID field
178-
6. Click on "Generate New Password" under "Application Secrets"
179-
7. Copy the generated password from the "New password generated" dialog and paste it to the App.config OneDriveConsumerApiClientSecret field and click OK
180-
8. Click on "Add Platform" under "Platforms"
181-
9. Click on "Web"
182-
10. Ensure the "Allow Implicit Flow" is checked, enter a URL in the "Redirect URIs" field. This can be any URL. Just beware that this URL will receive your access token, so use a site that belongs to you. I'll use https://apps.zomers.eu . Make sure "Live SDK support" at the bottom is checked. Click on "Save" at the bottom.
183-
11. Copy the same URI you've used at the previous step to the App.config OneDriveConsumerApiRedirectUri field
184-
12. Run the demo application and click on "Authorize"
185-
186-
### OneDrive for Business via Azure AD / tenant-my.microsoft.com
187-
188-
1. Go to https://manage.windowsazure.com
189-
2. Click on Active Directory in the left navigation bar
190-
3. Click on your Azure Active Directory name
191-
4. At the top, click on Applications
192-
5. At the bottom, click on Add
193-
6. Click on "Add an application my organization is developing"
194-
7. Enter any name you would like, choose "Web application and/or web api" as the type, *regardless whether or not you will be using the OneDrive API in a web application*
195-
8. As the sign-on URL, enter: https://login.live.com/oauth20_desktop.srf . As the App ID URI enter anything you would like. It must be in an URL format. I.e. https://apps.zomers.eu/myonedriveapiapp
196-
9. Once the application is created, click on Configure at the top
197-
10. If you want users outside of your own Azure Active Directory to be able to log in to the OneDrive API, switch "Application is multi-tenant" to YES. If only users from your own Azure Active Directory will be using the OneDrive API, keep it at NO.
198-
11. Click the green Add application button at the bottom
199-
12. Click on Office 365 SharePoint Online and then on the round checkmark buttom at the bottom right
200-
13. At the bottom under "permissions to other applications" ensure that for Windows Azure Active Directory under Delegated Permissions "Sign in and read user profile" is checked. Click on the dropdown for Delegated Permissions in the line that reads "Office 365 SharePoint Online" and select "Read and write user files"
201-
14. Click on Save at the bottom
202-
15. Under keys select 1 or 2 years in the dropdown and click Save again at the bottom
203-
16. After it's done processing your changes, it will show the client secret in the line under keys where you just used the dropdown. Copy this value to notepad. This is the client secret and only will be visible once and never again.
204-
17. Copy the Client ID field at the top.
205-
18. Replace the Client ID and Client Secret in your App.config where you will be using the OneDrive API. Use the DemoApplication its app.config for a sample of this.
206-
19. Run the demo application, select "OneDrive for Business O365" and click on "Authorize"
162+
## Register your own Client ID
163+
164+
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.
165+
166+
1. Go to https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
167+
2. At the top, click on New registration
168+
3. Enter any name for the application that you would like
169+
4. Under _Supported account types_ select _Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)_
170+
5. Under _Platform configuration_ select _Client Application_
171+
6. Hit the _Register_ button at the bottom
172+
7. Click on _Add a platform_ followed by clicking on _Mobile and desktop applications_
173+
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
174+
9. In the left menu bar, click on _Overview_
175+
10. Copy the _Application (client) ID_ from the section at the top
176+
11. Set the [ClientId](https://github.com/KoenZomers/OneDriveAPI/blob/f32d39891614d47efc9b5cb50d9500ca239751a0/Api/OneDriveApi.cs#L28) 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.
177+
12. Run the demo application, select "Graph API (Consumer & Business)" and click on "Authorize"
207178

208179
## Feedback
209180

0 commit comments

Comments
 (0)