【问题标题】:Google Drive Api get current userGoogle Drive Api 获取当前用户
【发布时间】:2017-04-09 20:40:21
【问题描述】:

我正在关注位于以下位置的 Google Drive Api 文档: https://developers.google.com/drive/v3/web/quickstart/dotnet

我已将文件夹更改为另一个特定文件夹。

我的代码

string credPath = Server.MapPath(@"~\googleDrive");
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;  

通过实现的表单,将始终使用相同的权限(第一个),因为它必须链接到“用户”字段。

我的问题:

  1. 如何搜索当前登录 Google 的帐户以将其放入 user 字段?
  2. 如果用户未登录,他无法访问任何内容,我该怎么办?
  3. 如果用户 x 不访问用户数据y,我是否应该更改user 字段或许可路径或两者兼而有之?
  4. 如果在下图的权限屏幕上,如果用户关闭浏览器或拒绝访问,页面不被锁定的最佳做法是什么?

【问题讨论】:

    标签: c# asp.net-mvc google-api google-drive-api


    【解决方案1】:
    1. 您无需查找当前用户的姓名即可在此处输入。 “用户”字段 - 只是凭据的快捷方式名称(文件名的一部分),将保存在文件数据存储中,与您在此处输入的内容无关,但每个文件数据存储必须是唯一的。李>
    2. 如果用户的凭据未存储在文件数据存储中,或者用户未登录,或已登录但未授予应用程序权限 - 应用程序无法访问任何内容
    3. 对于您要访问的 Google 驱动器的每个用户,您必须输入一个唯一的名称。不太清楚许可证路径是什么。
    4. 您可以尝试下面的代码,或者最好使用google samples here
    // Time-out in milliseconds, after which auth process will be terminated, whether permission granted or not
    var timeoutMs = 60*1000; 
    var credCts = new CancellationTokenSource(timeoutMs);
    var credPath = ".credentials/app-user-credentials";  //<-- In API refs ctor definead as FileDataStore(string folder, [bool fullPath = False]), credPath must be a folder name, not a file name as in your code
    var authTask = GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, Scopes, "someuniqueusername",
        credCts.Token, new FileDataStore(credPath, true));
    UserCredential credential = null;
    try
    {
        if (authTask.Wait(timeoutMs, credCts.Token))
        {
            credential = authTask.Result;
        }
        else
        {
            throw new OperationCanceledException("Auth time-out");
        }
    }
    catch (Exception ex)
    {
        Logger.Error(ex.Message);
        throw;
    }
    finally
    {
        credCts.Dispose();
        if (authTask.IsCanceled || authTask.IsCompleted || authTask.IsFaulted)
            authTask.Dispose();
    }
    
    
    // do some stuff...
    

    更新:
    在您成功收到UserCredentials(第一次,或存储在FileDataStore)后,获取授权用户的电子邮件,您可以使用此功能:

    private static string GetUserEmail2(UserCredential credential)
    {
        // Create Drive API service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Name-Of-Your-Google-App",
        });
    
        var about = service.About.Get().Execute();
    
        return about.User.EmailAddress;
    }
    

    【讨论】:

    • 你有 C# MVC 项目的示例吗?
    • 关于some unique username字段,用户第二次访问系统时如何搜索?还是我迷惑了,授权谷歌应用,用谷歌账号登录?
    • @MarlonTiedt 我正在通过有关如何获取授权用户电子邮件地址的步骤更新我的答案
    【解决方案2】:

    感谢https://stackoverflow.com/a/16168206/13513993 的解决方案,您必须使用 Outh2 API:-

    using Google.Apis.Oauth2.v2;
    using Google.Apis.Oauth2.v2.Data;
    

    添加范围(电子邮件):-

    public static string[] Scopes = { Google.Apis.Drive.v3.DriveService.Scope.DriveReadonly, "email" };
    

    您可以从以下位置获取凭证:-

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

    然后使用这些函数:-

    var oauthSerivce = new Oauth2Service(new BaseClientService.Initializer { HttpClientInitializer = credential });
    
    UserInfo =  oauthSerivce.Userinfo.Get().Execute();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多