如何在Google课堂中实施OAuth

如何在Google课堂中实施OAuth

本文介绍了如何在Google课堂中实施OAuth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用Google教室API来将教室集成到我的.NET产品中.我正在使用以下方法对用户进行身份验证.我的问题是,当我执行此代码时,它第一次要求进行身份验证,但是当我下次执行此代码时时间它直接以以前的登录凭据身份登录.当我在很多天后尝试登录时,许多浏览器也以第一个经过身份验证的用户身份直接登录.作为以前的用户凭据输入.如何实现此目标...?我是OAuth和API的新手.您的宝贵答案将对我的团队有很大帮助.

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

推荐答案

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

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

}

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

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;

这篇关于如何在Google课堂中实施OAuth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:26