Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google API authorisation with GoogleWebAuthorizationBroker: how to manage multiple users in web application?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading