How to access the Google API with different user accounts? (ASP.NET MVC)

Viewed 417

I am developing a web application in ASP.NET MVC (C #), which works with the Google Drive API. I've gotten the whole issue of authentication using OAuth 2.0 and I'm working perfectly with the API. The problem is that after logging in and giving my application permissions to work, if I access the URL of my website from another computer, it does not ask for authorization, but instead works with the account that had already been authenticated at first.

In other words, I need to be able to access my website and work with Google Drive with different user accounts. Where everyone authenticates and works with their Google Drive.

The code is the following:

public static Google.Apis.Drive.v3.DriveService GetService_v3()
{
    UserCredential credential;
    using (var stream = new FileStream(HostingEnvironment.MapPath("~/client_secret.json"), FileMode.Open, FileAccess.Read))
    {
        String FolderPath = HostingEnvironment.MapPath("~");
        String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");

        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(FilePath, 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;
}
1 Answers
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(FilePath, true)).Result;
    }

File data store stores the credeintals of the currently logged in user into %appdata% on the machine denoted by "user". So if you want more then one user then you need to change "user" to something else.

Please note that GoogleWebAuthorizationBroker.AuthorizeAsync is used for installed applications it will open the authorization browser on the users machine this can not be used for hosting a website it will attempt to open the web browser for the user to authenticate on the server which will not work.

use

private static readonly IAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow(new 

GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = "PUT_CLIENT_ID_HERE",
                        ClientSecret = "PUT_CLIENT_SECRET_HERE"
                    },
                    Scopes = new[] { DriveService.Scope.Drive },
                    DataStore = new FileDataStore("Drive.Api.Auth.Store")
                });

web-applications-asp.net-mvc

Related