Search code examples
c#asp.netgoogle-apigoogle-oauthgoogle-api-dotnet-client

How to change authentcated users to the Google Drive API


I have followed .NET Quickstart tutorial to fetch the drive name list from the Google Drive.

This code is working fine. however, I want to know if I can force the authentication again (something like log out, but I think google isn't call it like that).

Currently I need to wait the consent to time out in order to select another account.

(If I start the application first time or the previous session has timed out, a Google consent page will be prompted so that I can select or add a particular account to authenticate).

I am using OAuth2 and the platform is .NET window form application.


Solution

  • It helps if you post all of your code wrather then just linking a tutorial you followed.

    The way the Google API dotnet client library works is that it stores the user credentials in a datastore on your machine. In the following section of code "user" denotes a users credentials if you change that you can force it to swap between users. If a user does not have credentials then it will pop up and ask you for authentication again. To fully understand how filedata store works I recommend you read my tutorial on the subject Google .net – FileDatastore demystified

    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
    

    In the world of Oauth there really is no such thing as logging out. Technically speaking you have access to a users data until they remove that access or something happens to your refresh token. It will be up to you to change the "user" in your code to make it appear that the user has logged out on your site. The only thing that is close to this is Revoking the token which is basically removing your access to a users data. the user will have to authenticate your application again if they chose to login again.