I am trying to create my own implementation of IDataStore for use with Google Authentication.
I have created the DatabaseDataStore (similar to one found in the sample) which implements the IDataStore. 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.
var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()). AuthorizeAsync(cancellationToken);
and this is AppFlowMetadata :
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”,””,””,””,””)
});
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.
{“access_token”:”xxxxxx”,”token_type”:”Bearer”,”expires_in”:3600,”refresh_token”:”xxxxxx”,”Issued”:”2015-04-08T18:17:01.233+05:30″}
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.
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;
}
I have created the class for StoredResponse as below. ( I just referred the article for savedDatastore.)
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();
}
}
This doesn't prompt for login, but credentials are null.
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.