Google .net – FileDatastore demystified 12


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.

[wp_ad_camp_3]

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.

[wp_ad_camp_5]

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.


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

12 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?

  • Rosen

    Hi,

    Thanks for your publication. I’m developing asp.net application which uses IIS server and when I start it, the execution flow freezes in the part of code:

    credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
    secrets, //GoogleClientSecrets.Load(stream).Secrets,
    new[]
    {
    YouTubeService.Scope.YoutubeReadonly,
    YouTubeService.Scope.YoutubeUpload,
    YouTubeService.Scope.Youtube,
    YouTubeService.Scope.YoutubeForceSsl,
    YouTubeService.Scope.Youtubepartner,
    YouTubeService.Scope.YoutubepartnerChannelAudit
    },
    “user”,
    CancellationToken.None,
    new FileDataStore(refresh_token_path)
    ).Result;

    Then I change it with this one:

    IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
    new GoogleAuthorizationCodeFlow.Initializer
    {
    ClientSecrets = secrets,
    DataStore = new FileDataStore(refresh_token_path),
    Scopes = new[] {
    YouTubeService.Scope.YoutubeReadonly,
    YouTubeService.Scope.YoutubeUpload,
    YouTubeService.Scope.Youtube,
    YouTubeService.Scope.YoutubeForceSsl,
    YouTubeService.Scope.Youtubepartner,
    YouTubeService.Scope.YoutubepartnerChannelAudit
    }
    });

    As result, the code flow continues its execution but there is no created file in the specified path, which contains folder with name “App_Data”. Could you give me, please an appointment what is the reason for this and how can I fix it?
    I should also tell you that when I start this code in a test console application it works fine in both cases.

    Thanks in advance!

  • Kalyani S

    Hi, I am not able to create folder “Roaming” under path C:\Users\kalyani.s\AppData\,where we write “Google.Apis.Auth.OAuth2.Responses.TokenResponse-user” file in production environment.
    Can I read Google.Apis.Auth.OAuth2.Responses.TokenResponse-user file using any method and then write it to path I want.?