Google .net – FileDatastore demystified 4


Please Share

Google APIsThe Google .net client library comes with FileDataStore as a default datastore.

File data store (FileDataStore.cs) that implements IDataStore. This store creates a different file for each combination of type and key. This file data store stores a JSON format of the specified object.

I have had a number of people contacting me over the last several months asking me different questions about FileDataStore.

  1. What is FileDataStore?
  2. Where is my refreshToken?
  3. How do I change the folder where authentication is stored.

What does FileDataStore do exactly?

Lets look at FileDataStore.   When the following code authenticates. A folder called Drive.Auth.Store will be created in the %AppData% directory on the machine executing the code.

So we will have a new directory called  %AppDatat%\Drive.Auth.Store .  When I check my machine I find it here C:\Users\lindaHP\AppData\Roaming\Drive.Auth.Store

UserCredential credential;
using (var stream = new FileStream(clientSecretsJsonFilePath
                                   ,FileMode.Open
                                   ,FileAccess.Read))
      {   
      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets,
      new[] { DriveService.Scope.Drive,  DriveService.Scope.DriveFile },
      "LookIAmAUniqueUser",
       CancellationToken.None,
      new FileDataStore("Drive.Auth.Store")                               
      ).Result;
      }

Assuming the user clicks accept on the authentication request screen, a new file will be created in that directory with the following structure:

Google.Apis.Auth.OAuth2.Responses.TokenResponse-LookIAmAUniqueUser.TokenResponse-LookIAmAUniqueUser

Each user will have their own file you change a user by changing the “LookIAmAUniqueUser” value.

The file contains all the information you need to gain access to this users account.

{  
   "access_token":"ya29.4wEk2VsbqiPfR4oH5WaYo7aYgAmlP2KSIl-heyDnPBBHMYYKnfU6YuQ-_RsDofD8QR1T",
   "token_type":"Bearer",
   "expires_in":3600,
   "refresh_token":"1/PSvxzxGB-3XU8bF2SrG6llzO-ZizE4mftrd9Edqbubg",
   "Issued":"2015-09-03T11:43:47.681+02:00"
}

Specify Folder

What if we don’t want the files stored in %AppData%? What if i want to specify my own folder to store the dataStore? Well in that case we can tell FileDatatStore to put it in a different Folder.

UserCredential credential;
using (var stream = new FileStream(clientSecretsJsonFilePath
                                   ,FileMode.Open
                                   ,FileAccess.Read))
      {   
      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets,
      new[] { DriveService.Scope.Drive,  DriveService.Scope.DriveFile },
      "LookIAmAUniqueUser",
       CancellationToken.None,
      new FileDataStore(@"c:\datastore",true)                              
      ).Result;
      }

The code above will store the same file only this time instead of being in %appdata%\Drive.Auth.Store they will be in c:\datastore

An alternate way of doing this would be to supply the folder to GoogleWebAuthorizationBroker before you call it, then you can omit FileDataStore completely because by default GoogleWebAuthorizationBroker.AuthorizeAsync uses FileDataStore.


UserCredential credential;
using (var stream = new FileStream(clientSecretsJsonFilePath
                                   ,FileMode.Open
                                   ,FileAccess.Read))
      {   
      GoogleWebAuthorizationBroker.Folder = @"c:\temp\datastore" ;     
      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets,
      new[] { DriveService.Scope.Drive,  DriveService.Scope.DriveFile },
      "LookIAmAUniqueUser",
       CancellationToken.None                           
      ).Result;
      }

Conclusion

By default GoogleWebAuthorizationBroker users FileDataStore to store the user authentication data. By Default FileDataStore stores this information in the %appData% direcotry of the current machine. If we want to change the directory where the authentcation is stored we can either use GoogleWebAuthorizationBroker.Folder or specify a path to FileDataStore.

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog www.daimto.com. This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.


Leave a comment

Your email address will not be published. Required fields are marked *

4 thoughts on “Google .net – FileDatastore demystified

  • Martin

    Hello, I’m developing an application for my final project in Software Engineering and I’ll have the application’s users logged with credentials in the server we’ll set but it has’t enough storage memory and for that constraint we thought about every user could see their files of their own google drive through the application in a sort of list which shows name of file and type (.doc, .xls, etc).
    Even I’ve been reaserching a lot in the documentation, I couldn’t find the way of associate the google credentials of the user with the application in order to show by the application the files of their personal drive. I went on with steps detailed getting google developer credentials but in that way, users only can get into my google drive’s files (credential’s owner).

  • darshan dave

    I have virtual server path and i am trying to store credential file in virtual directory path but not able to do that
    so my question is, is FileDataStore can store file in virtual directory or not?

    • Linda Lawton
      Linda Lawton Post author

      No idea really I haven’t tried but this might explain why so many people have problems with getting it to work with IIS. try asking this question on Stackoverflow.