{"id":6968,"date":"2019-04-23T18:17:18","date_gmt":"2019-04-23T18:17:18","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6968"},"modified":"2019-04-24T08:51:03","modified_gmt":"2019-04-24T08:51:03","slug":"adding-onedrive-support-to-an-application","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/adding-onedrive-support-to-an-application\/","title":{"rendered":"Adding OneDrive support to an application"},"content":{"rendered":"<p>I wanted to create an application which stores data for synchronisation between devices, so figured the cheapest way might be to use one of the free storage mediums, in this case OneDrive.<\/p>\n<ul>\n<li>Create an application, mine&#8217;s a WPF app.<\/li>\n<li>Add the NuGet package Microsoft.OneDriveSDK<\/li>\n<li>Add the NuGet package Microsoft.OneDriveSdk.Authentication<\/li>\n<\/ul>\n<p><em>Note: Windows allows the storage of data to the user&#8217;s roaming profile to be stored in the &#8220;cloud&#8221;. So if your application is solely Windows based this might be a better solution than using OneDrive directly.<\/em><\/p>\n<p><strong>Registering our application<\/strong><\/p>\n<p>Before we get into any coding we&#8217;ll need to register our application with the Microsoft Application Registration Portal, instructions can be found <a href=\"https:\/\/docs.microsoft.com\/en-gb\/onedrive\/developer\/rest-api\/getting-started\/app-registration?view=odsp-graph-online\" rel=\"noopener noreferrer\" target=\"_blank\">here<\/a>.<\/p>\n<p>Basically you&#8217;ll be prompted to log into the Microsoft Application Registration Portal, then you should Add your application, supplying it with a name. Once completed, this will supply us with an application id and application secrets. <\/p>\n<p>As my sample application is a native application then I don&#8217;t need to create an app password.<\/p>\n<p>Just click Add Platform and add the platforms you are wanting to support (again mine&#8217;s a simple native application). <\/p>\n<p><strong>Writing some code<\/strong><\/p>\n<p>First off we&#8217;ll need to authenticate the user, so we use an <a href=\"https:\/\/github.com\/OneDrive\/onedrive-sdk-dotnet-msa-auth-adapter\" rel=\"noopener noreferrer\" target=\"_blank\">authentication adapter<\/a>. Here&#8217;s my code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar msaAuthenticationProvider = new MsaAuthenticationProvider(\r\n   AppId,\r\n   &quot;https:\/\/login.live.com\/oauth20_desktop.srf&quot;,\r\n   new&#x5B;] { &quot;onedrive.readwrite&quot;, &quot;offline_access&quot; });\r\n<\/pre>\n<p>When you run this code, you&#8217;ll be presented with a login screen requiring username\/email and password, then a dialog to tell you your application is requesting permissions to access OnDrive and asking you to grant or deny them.<\/p>\n<p>Authorisation scopes, such as <em>onedrive.readonly<\/em> can be found <a href=\"https:\/\/docs.microsoft.com\/en-us\/onedrive\/developer\/rest-api\/getting-started\/msa-oauth?view=odsp-graph-online#authentication-scopes\" rel=\"noopener noreferrer\" target=\"_blank\">here<\/a>. <\/p>\n<p>Now we need to authenticate and access the one drive client. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nawait msaAuthenticationProvider.AuthenticateUserAsync();\r\n\r\nvar oneDriveClient = new OneDriveClient(\r\n   &quot;https:\/\/api.onedrive.com\/v1.0&quot;, \r\n   msaAuthenticationProvider);\r\n<\/pre>\n<p>Once we get an instance of the OneDriveClient we can interact with the One Drive files\/file system. Here&#8217;s an example snippet of code which gets to a know folder location, for example imagine we have a folder named Public\/MyApp then we could use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar item = await \r\n   oneDriveClient\r\n      .Drive\r\n      .Root\r\n      .ItemWithPath(&quot;Public\/MyApp&quot;)\r\n      .Request()\r\n      .GetAsync();\r\n<\/pre>\n<p>We can also use a &#8220;special folder&#8221; which the application data is stored to by changing the permission scope to <em>onedrive.appfolder<\/em>, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar msaAuthenticationProvider = new MsaAuthenticationProvider(\r\n   AppId,\r\n   &quot;https:\/\/login.live.com\/oauth20_desktop.srf&quot;,\r\n   new&#x5B;] { &quot;onedrive.appfolder&quot;, &quot;offline_access&quot;);\r\n\r\n\r\nawait msaAuthenticationProvider.AuthenticateUserAsync();\r\n\r\nvar item = await oneDriveClient\r\n   .Drive\r\n   .Special\r\n   .AppRoot\r\n   .Request()\r\n   .GetAsync();\r\n<\/pre>\n<p><strong>Further Reading<\/strong><\/p>\n<p><a href=\"https:\/\/developer.microsoft.com\/en-us\/onedrive\" rel=\"noopener noreferrer\" target=\"_blank\">OneDrive Dev Center<\/a><br \/>\n<a href=\"https:\/\/github.com\/onedrive\" rel=\"noopener noreferrer\" target=\"_blank\">Official projects and SDKs for Microsoft OneDrive<\/a><br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/onedrive\/developer\/rest-api\/concepts\/special-folders-appfolder?view=odsp-graph-online\" rel=\"noopener noreferrer\" target=\"_blank\">Using an App Folder to store user content without access to all files<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to create an application which stores data for synchronisation between devices, so figured the cheapest way might be to use one of the free storage mediums, in this case OneDrive. Create an application, mine&#8217;s a WPF app. Add the NuGet package Microsoft.OneDriveSDK Add the NuGet package Microsoft.OneDriveSdk.Authentication Note: Windows allows the storage of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[241],"tags":[],"class_list":["post-6968","post","type-post","status-publish","format-standard","hentry","category-onedrive"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6968"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6968\/revisions"}],"predecessor-version":[{"id":6974,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6968\/revisions\/6974"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}