0

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;
        }
4

1 回答 1

0

即使您不传入数据存储对象,默认情况下库也会将用户的凭据存储在C:\Users\%USERNAME%\AppData\Roaming\Google.Apis.Auth\. 如果您根本不想存储身份验证信息,而是让用户在每次运行时授权应用程序,则需要传入一个实际上不存储凭据的自定义数据存储对象:

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);
    }

}

然后将此类的一个实例传递给该AuthorizeAsync()方法:

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    Scopes,
    "user",
    CancellationToken.None,
    new NullDataStore()).Result;
于 2015-08-19T15:18:44.317 回答