title: Authenticating Google Drive using tokens saved in the Database
body: <p>I am trying to create my own implementation of  <a href="https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/Apis/Util/Store/IDataStore.cs?r=83898571318b2f70482376fdb0e106ac0f003fd5" rel="nofollow">IDataStore</a> for use with Google Authentication.</p>
  
  <p>I have created the DatabaseDataStore (similar to one found in the <a href="https://github.com/LindaLawton/Google-Dotnet-Samples/blob/master/Authentication/Diamto.Google.Authentication/Diamto.Google.Authentication/DatabaseDataStore.cs" rel="nofollow">sample</a>) which implements the <a href="https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/Apis/Util/Store/IDataStore.cs?r=83898571318b2f70482376fdb0e106ac0f003fd5" rel="nofollow">IDataStore</a>. In my web application I have a register User page where I ask user to click the register button and he gets redirected to the Google authentication. This is the code for that.</p>
  
  <pre><code>var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()). AuthorizeAsync(cancellationToken);
  </code></pre>
  
  <p>and this is AppFlowMetadata :</p>
  
  <pre><code>private static readonly IAuthorizationCodeFlow flow =
  new CustomAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
      {
      ClientSecrets = new ClientSecrets  { ClientId = “my client id”, ClientSecret = “my client secrets” },
      Scopes = new[] { DriveService.Scope.Drive },
      DataStore = new DatabaseDataStore(“LocalDB”,””,””,””,””)
  });
  </code></pre>
  
  <p>I pass null empty values to the idatastore as I have hardcoded the connection string . After successful Oauth Flow I can see the data being saved in my database in GoogleUser Table. The google usertable saves this information automatically in refreshtoken field along with user id. </p>
  
  <blockquote>
    <p>{“access_token”:”xxxxxx”,”token_type”:”Bearer”,”expires_in”:3600,”refresh_token”:”xxxxxx”,”Issued”:”2015-04-08T18:17:01.233+05:30″}</p>
  </blockquote>
  
  <p>Now next step is that I want to user to go to another page in application where he will select the file to upload. Then he should be able to upload the file as he has already authenticated. Here I did something like this.</p>
  
  <pre><code>private static readonly IAuthorizationCodeFlow flow =
        new CustomAuthorizationCodeFlowSaved(new GoogleAuthorizationCodeFlow.Initializer {  ClientSecrets = new ClientSecrets { ClientId = “My client id”,ClientSecret = “my client Secret” },
        Scopes = new[] { DriveService.Scope.Drive },
        DataStore = new DatabaseDataStore(getStream())    
  } );
  private static StoredResponse getStream()
      {
      DataAccess objDA = new DataAccess();
      DataRow dr = objDA.getSingleDataRow(“Select RefreshToken from dbo.GoogleUser where userid=1 and username=’user Name'”);
      StoredResponse myStoredResponse = new StoredResponse(dr[“RefreshToken”].ToString());
      return myStoredResponse;
      }
  </code></pre>
  
  <p>I have created the class for StoredResponse as below. ( I just referred the article for savedDatastore.)</p>
  
  <pre><code>public class StoredResponse
  {
  public string access_token { get; set; }
  public string token_type { get; set; }
  public long? expires_in { get; set; }
  public string refresh_token { get; set; }
  public string Issued { get; set; }
  
  public StoredResponse(string pRefreshToken)
  {
  //this.access_token = pAccessToken;
  // this.token_type = pTokenType;
  //this.expires_in = pExpiresIn;
  this.refresh_token = pRefreshToken;
  //this.Issued = pIssued;
  this.Issued = DateTime.MinValue.ToString();
  }
  public StoredResponse()
  {
  this.Issued = DateTime.MinValue.ToString();
  }
  
  }
  </code></pre>
  
  <p>This doesn't prompt for login, but credentials are null. </p>
  
  <p>Can you please help me with this? ultimately I want to store the tokens to the database and use them after some time on another page. But not in one session. </p>