Generally in SharePoint Online, until we provide an O365 license to any user account, it will not be identified by SPO. To do some generic tasks such as downloading files, or reading some list, we try to avoid regular user accounts and try to use generic accounts. But unlike SP on-premise, we need to assign an O365 license to use this account in SPO.
Another approach we can use is by using a SharePoint App concept. With the help of this, we can do communicate with the SPO site and do things such as reading lists, downloading files, etc.
This approach basically generates an access token which will be used by the SharePoint App. This access token will be validated by the SharePoint online site.
I am not explaining how to register an SP app and providing that particular app permission on the SharePoint online site. Comment if you need any assistance on that. I will be happy to include that also.
I have created this simple console application which can be used and modified as per need. Please create a simple C# console application and include this file.
The first thing which we need to do is to generate access toke with the help of app id and app secret.
We need the below variables values to be identified and replaced in the code to work.
  1. tenantID – You can find tenant id from – Azure Active Directory à Properties
  2. strAppID – This is the App Guid which will be created at https://<sitename>
  3. strAppSecret – App secret
  4. strOrgDomain – Your organization domain will be used in “{organisationdomain}”;
Once these values are updated, there will be a web request created to get the access token which will be used by the SP app.
Once access toke is generated, we can use .NET’s WebClient class to download the file. Please update the below variables in the code
string webUrl = “Your SPO site url”
string documentLibName = This should be the path of the file. In the given example I have taken ‘Shared%20Documents/test’ where ‘test’ is a folder inside library.
string fileName = “file name with extension”;
You can refer to my Git Repository for the code.
The code is fairly straight forward and I have put the comments for better understanding.