I am calling Google Api authentication using this function
public static Google.Apis.Drive.v3.DriveService GetService()
{
//get Credentials from client_secret.json file
UserCredential credential;
var CSPath = MyServer.MapPath("Content");
using (var stream = new FileStream(Path.Combine(CSPath, "client_secret.json"), FileMode.Open, FileAccess.Read)) {
string credPath = MyServer.MapPath("content/token");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
//create Drive API service.
Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
}
It is storing Token in content\token folder under the application.
I am using this function to retrive some files from user Google Drive.
It is working fine for one user, but I want to implement it in a we app so that each user will retrieve the file from his own Google Drive account.
This senario is working well with Google File Picker
My question is to allow each user to use hios own google drive account
CodePudding user response:
GoogleWebAuthorizationBroker.AuthorizeAsync uses fileDatastore by default it stores the user credentials in the machines %appdata% folder. The name of the file it creates for the users credentials is denoted by "user".
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;
}
would result in
Google.Apis.Auth.OAuth2.Responses.TokenResponse-LookIAmAUniqueUser.TokenResponse-LookIAmAUniqueUser
Google .net – FileDatastore demystified
So if you change "user" you will be storing it as a different user. As you are running this as an installed application you will need to implement some way for your desktop app to know its a different user. Then just send a different string for each user.