本地保存的凭据google drive api 3

时间:2016-03-30 02:32:17

标签: c# google-drive-api google-oauth

我正在用C#创建一个桌面应用程序。我第一次启动我的应用程序一切顺利,他们让我登录和许可。现在我无法使用其他帐户登录,因为他们在本地保存了所有内容。我尝试删除〜/ .credentials / drive-dotnet-quickstart.json中的文件,是的,但只有在他们重新保存凭据后才会再次运行,但是在其他地方我找不到哪里。我希望能够从不同的帐户登录,因为我还需要更改权限问题的范围,我必须删除以前保存的凭据。我有登录的方式。

using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {

            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
        }
        Console.WriteLine(credential.ToString());
        service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

如何删除保存的凭据? 如何禁用凭据保存机制?

谢谢。

2 个答案:

答案 0 :(得分:0)

你可以"退出"通过调用等待credential.RevokeTokenAsync()或使用GoogleWebAuthorizationBroker.AuthorizeAsync中的其他用户ID

using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
    var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    {
        ClientSecretsStream = stream,
        Scopes = new[] { DriveService.Scope.DriveReadonly },
        DataStore = new FileDataStore(credPath, true)
    });

    credential = new UserCredential(flow, userId,
        await flow.LoadTokenAsync(userId, CancellationToken.None)
    );

    bool res = await credential.RevokeTokenAsync(CancellationToken.None);
    //credential = await GoogleWebAuthorizationBroker
    //    .AuthorizeAsync(
    //        stream,
    //        new[] { DriveService.Scope.DriveReadonly },
    //        userId,
    //        CancellationToken.None,
    //        new FileDataStore("oauth/drive"))
    //    ;
    //Console.WriteLine("Credential file saved");
}

检查凭证是否存在于本地

await new FileDataStore(credPath, true).GetAsync<TokenResponse>(userId) != null
//or
await flow.LoadTokenAsync(userId, CancellationToken.None) != null

答案 1 :(得分:0)

只需删除“ credPath”文件夹中的令牌文件