小编Sky*_*cer的帖子

无需浏览器的 Google 驱动 OAuth2

我正在尝试通过Google drive OAuth2我的 C# 桌面应用程序验证我的登录。当用户输入Client ID和时Client secret,浏览器将打开并要求确认。当用户输入错误时,浏览器也会打开Client ID,并Client secret在 Google 网页上显示错误消息...

那么,如何在没有浏览器确认的情况下进行身份验证并在代码中处理所有 google OAuth2 错误并且无需任何浏览器页面?

这是我的验证码:

//...code
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile };

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets
    {
        ClientId = "WrongID",
        ClientSecret = "WrongSecret"
    },
    scopes, _authUserName,
    CancellationToken.None, new FileDataStore(_authDataStorePath, true)).Result;
//...code
Run Code Online (Sandbox Code Playgroud)

当这个块中发生错误时,代码的执行就会停止,什么也不会发生。

更新1:

好吧,假设我知道如何获取带有令牌的 json 文件。现在,我如何用这个文件创建 DriveService 对象?现在我创建它如下:

DriveService _drive = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential, // How to make this credential …
Run Code Online (Sandbox Code Playgroud)

c# google-api google-drive-api

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

google-api ×1

google-drive-api ×1