代码之家  ›  专栏  ›  技术社区  ›  Kemal AL GAZZAH

Google API授权与Google WebAuthorizationBroker:如何在web应用程序中管理多个用户?

  •  1
  • Kemal AL GAZZAH  · 技术社区  · 2 年前

    我使用这个函数调用Google Api身份验证

    public static Google.Apis.Drive.v3.DriveService GetService()
            {
                //get Credentials from client_secret.json file 
                UserCredential credential;
                var CSPath = MyServer.MapPath("Content");
    
                using (var stream = new FileStream(Path.Combine(CSPath, "client_secret.json"), FileMode.Open, FileAccess.Read))            {
                    
                    string credPath = MyServer.MapPath("content/token");
    
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.FromStream(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(credPath, true)).Result;
                }
    
                //create Drive API service.
                Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "GoogleDriveRestAPI-v3",
                });
                return service;
            }
    

    它将令牌存储在应用程序下的content\Token文件夹中。

    我正在使用这个功能从用户Google Drive中检索一些文件。

    对于一个用户来说,它工作得很好,但我想在一个we应用程序中实现它,这样每个用户都可以使用它 从他自己的Google Drive帐户中检索文件。

    这位塞纳里奥和他合作得很好 Google File Picker

    我的问题是允许每个用户使用hios自己的google drive帐户

    1 回复  |  直到 2 年前
        1
  •  1
  •   Linda Lawton - DaImTo    2 年前

    UserCredential credential;
    using (var stream = new FileStream(clientSecretsJsonFilePath
                                       ,FileMode.Open
                                       ,FileAccess.Read))
          {   
          credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
          GoogleClientSecrets.Load(stream).Secrets,
          new[] { DriveService.Scope.Drive,  DriveService.Scope.DriveFile },
          "LookIAmAUniqueUser",
           CancellationToken.None,
          new FileDataStore("Drive.Auth.Store")                               
          ).Result;
          }
    

    会导致

    谷歌。API。啊。OAuth2。响应。TokenResponse LookiaMonuiqueuser。TokenResponse LookiaMonuiqueuser

    Google .net – FileDatastore demystified

    所以,如果你改变了“用户”,你将把它存储为一个不同的用户。当你把它作为一个已安装的应用程序运行时,你需要实现一些方法,让你的桌面应用程序知道它是一个不同的用户。然后为每个用户发送不同的字符串。