Search code examples
google-oauthgoogle-classroom

How to implement OAuth in google classroom


Presently i am working on google classroom API to integrate classroom into my .NET product.I am using below method for authenticating user.My problem is when i execute this code it asking authentication for first time but when i execute this code next time it directly log in as previous log in credentials.When i try this after many days and many browsers also directly log in as first authenticated user.But for every fresh time execution of code i want it ask for authentication of user rather than directly log in as previous user credentials.How to achieve this...? I am new to this OAuth and API's.Your valuable answer will help my team a lot.

please any one help me on this...

private ClassroomService getservice()
        {
            using (var stream =
              new FileStream(Server.MapPath("client_secret1.json"), FileMode.Open, FileAccess.Read))
            {

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                      CancellationToken.None).Result;
            }
            var service = new ClassroomService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });
            return service;
        }

Solution

  • Even if you don't pass in a data store object, the library by default will store the user's credentials in C:\Users\%USERNAME%\AppData\Roaming\Google.Apis.Auth\. If you don't want to store the auth information at all, and instead have the user authorize the application on each run, you'll need to pass in a custom data store object that doesn't actually store the credentials:

    class NullDataStore : IDataStore
    {
        public Task StoreAsync<T>(string key, T value) 
        {
            return Task.Delay(0);
        }
    
        public Task DeleteAsync<T>(string key)
        {
            return Task.Delay(0);
        }
    
        public Task<T> GetAsync<T>(string key)
        {
            return Task.FromResult(default(T));
        }
    
        public Task ClearAsync()
        {
            return Task.Delay(0);
        }
    
    }
    

    Then pass an instance of this class into the AuthorizeAsync() method:

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