Welcome to ASP.NET Guild

Be sure to come back often and tell others. If you have any tips, tricks, examples, please email them to me at chris.williams@techguilds.com and I will post them. Check out our ASP.NET QuickStart and C# QuckStart Libraries. Below is my latest articles.

Friday, December 15, 2017

Reading and Writing files to Google Drive

It has been a while since I posted but I have been playing with Google Drive and accessing it from a console application and had to search in a few places to find the right way to do it. The documentation tells you to download the .JSON file and that is helpful.  You can find the sample on how to get a list of files from Google Drive in a Console application.

This is a good start but I was getting errors related to the Certificate:

"At least one client secret (Installed or Web) should be set"

The problem is that the code provided has you using UserCredentials and some funky streaming stuff.  The credentials can be read from the stream like this into a Google Credentials object.

            GoogleCredential credential = null;

            using (var stream = new FileStream(credentialsFileName, FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream);
                credential = credential.CreateScoped(scopes);
            }

You then use that Google Credentials along with your application name to get the service:

            // Create Drive API service.
            return new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = applicationName,
            });

Now after that fix, the original example works. I am assuming that since the article was written this has changed and the article has not been updated.  After applying this I do get the list of files from the article.

Scopes

A scope is what determines what the calls made using the service can do. In general there are 2 scopes: Read-Only and Full Access. There are other scopes you can use as well.

If you want to do more than read from the drive then you will need to change your scope.  

        string[] ReadOnlyScope = { DriveService.Scope.DriveReadonly }; // Read Only
        public static string[] FullAccessScope = { DriveService.Scope.Drive }; // Full Access


Permissions

It took a while to find how permissions work but I did find an article on Managing Shares that does go into it. It boils down to 2 types of permission: user and domain. A user permission assigns an individual user access. A domain permission assigns a domain access eg. "sitecore.net".  A permission has a role. The article describes the roles as "reader" and "writer".  This is a text string so possibly there are other roles as well.

Now personally, I have got the user permissions to work but when I try domain permissions I get an error. If you have successfully got domain working can you provide an example or take a look at the functions on GitHub for Domain Permission to see if there is something I am doing wrong?  I am passing "readwatchcreate.com" as domain. 

GitHub Libraries

I have created a GoogleAPIs public repository on GitHub. There are 2 folders. One for those still using .NET Framework which will not be updated and a DotCore folder with the new version built on .NET Standard and .NET Core. This includes a base library that has the code to connect to the API, a library that wraps some Google Drive functions to help you, and a unit test console app that will show sample calls and do positive and negative testing of the library. As I continue my learning of Google APIs, I will be adding more libraries for GoogleSheet and other API sections.


Are you a .NET Developer or Contractor interested in working with Sitecore or Dynamics CRM?

Apply for our Mentorship Program. If accepted, we will mentor you on Sitecore and provide you with project to help you build your skills and make some money at the same time. If you are interested send your resume with details on why you want to work with Sitecore or Dynamics CRM to: Chris Williams - chris.williams@techguilds.com or Dennis Augustine - dennis.augustine@techguilds.com We look forward to working with you to achieve your goals.