我可以使用C#在浏览器的Google驱动器中查看文件吗

阿吉斯·托马斯(Ajith K Thomas)

我可以使用C#在浏览器中的Google驱动器中查看文件吗,我已经尝试过使用Google API。但总是要求输入登录详细信息。如何使用C#在浏览器中查看文件?

我在这里尝试过

public ActionResult GetDriveFile()
    {
        string filePath = System.Web.HttpContext.Current.Server.MapPath("~/client_secret.json");
        //string filePath = System.Web.HttpContext.Current.Server.MapPath("~/MyProject-e09c7c94100a.json");
        string userName = "[email protected]";

        //GoogleCredential credential;
        //string[] Scopes = { Google.Apis.Drive.v2.DriveService.Scope.Drive };

        //using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        //{
        //    credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
        //}

        //var driveService = new Google.Apis.Drive.v2.DriveService(new BaseClientService.Initializer()
        //{
        //    HttpClientInitializer = credential,
        //    ApplicationName = "Test",
        //});


        var driveService = AuthenticateOauth(filePath, userName);

        List<Google.Apis.Drive.v3.Data.File> allFiles = retrieveAllFiles(driveService);

        string fileID = allFiles.Where(x => x.Name.Contains("sample.jpg")).Select(y => y.Id).FirstOrDefault();

        //DownloadFile(driveService);

        string fileLink = GetFileLink(fileID);

        return Redirect(fileLink);
    }



public static Google.Apis.Drive.v3.DriveService AuthenticateOauth(string clientSecretPath, string userName)
    {
        try
        {
            if (string.IsNullOrEmpty(userName))
                throw new Exception("userName is required.");
            if (!System.IO.File.Exists(clientSecretPath))
                throw new Exception("clientSecretJson file does not exist.");

            // These are the scopes of permissions you need. It is best to request only what you need and not all of them
            string[] scopes = new string[] { Google.Apis.Drive.v3.DriveService.Scope.Drive };                   // View and manage the files in your Google Drive         
            UserCredential credential;
            using (var stream = new System.IO.FileStream(clientSecretPath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                credPath = System.IO.Path.Combine(credPath, ".credentials/apiName");

                // Requesting Authentication or loading previously stored authentication for userName
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                                                                         scopes,
                                                                         userName,
                                                                         CancellationToken.None,
                                                                         new Google.Apis.Util.Store.FileDataStore(credPath, true)).Result;
            }

            // Create Drive API service.
            return new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Drive Authentication Sample",
            });
        }
        catch (Exception ex)
        {
            Console.WriteLine("Create Oauth2 DriveService failed" + ex.Message);
            throw new Exception("CreateOauth2DriveFailed", ex);
        }
    }

拜托,有人对此有任何想法吗?

戴姆敦

当您使用权限与用户共享文件时:创建时,可以选择向他们发送通知。这告诉他们文件已与他们共享。

现在,文件已与它们共享。File.get返回文件资源注释,请确保您添加fields = *,否则您将不会从file.get中获得很多元数据。

webViewLink
用于在相关的Google编辑器或浏览器中打开文件的链接。

基本上,如果有问题的用户具有访问权限,那么您将获得此链接回到文件。像这样

https://docs.google.com/document/d/xWau_bNrntLBDDFjwUPBDkL3_oBW3hthavaVF8Ed1mbA

我还无法弄清楚如何在网站上创建一个可共享的链接,因此我想也可以通过API来实现。目前,我认为该文件必须是公开的,或者用户必须具有访问权限,然后您才能获得Webview链接。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我可以使用 C# 在浏览器中查看和打开谷歌驱动器中的文件吗

Android:即使可以在浏览器中查看文件,也可以使用java.io.FileNotFoundException

Google员工可以查看保存在Google Chrome浏览器中的我的密码吗?

无法在浏览器中查看Ubuntu Server(VM),我可以使用SSH连接

我可以使用Revo Uninstaller从驱动器D(不是C的分区)中删除程序吗?

我的空间不足,无法提取 .rar 文件,我使用 C: Drive 下载了文件,我可以使用外部硬盘驱动器提取文件吗?

我可以使用JavaScript在Chrome浏览器中显示HTML元素吗?

如何检查用户的浏览器是否可以使用JavaScript查看DJVU文件

我可以在服务器端使用GoogleIdToken在Google驱动器上创建文件吗?

我可以使用符号链接将C:\ Users“移动”到另一个驱动器吗?

合作:我可以访问我的Google驱动器文件夹和文件吗?

我可以使用虚拟磁盘驱动器安装Windows吗?

我可以使用低容量型号的硬盘驱动器PCB吗?

我们可以使用javascript获取安装在浏览器中的搜索引擎列表吗?

我可以使用JS中HTML5电子邮件验证内置的浏览器吗?

我可以使用某些查看器来查看和搜索serilog日志文件吗?

我可以使用Spring Security管理多个浏览器选项卡吗?

我可以使用TestCafe进行非浏览器测试吗?

我们可以使用javascript将图像推送到浏览器缓存吗

我可以使用客户端(浏览器)IP作为cURL的代理吗?

我可以使用多个@hostlistener或基于浏览器的事件吗?

我可以使用浏览器扩展来操纵URL参数吗?

您可以使用Angular的HttpClient强制浏览器下载文件吗?

我可以将网络文件夹与Google驱动器同步吗?

我可以在C驱动器之外的其他驱动器上安装Chocolatey吗?

为什么文件浏览器没有显示我的映射驱动器?

我们可以使用C#在FTP服务器中解压缩文件吗

我可以使用连接到用作交换机的路由器的网络驱动器吗?

使用Google Chrome浏览器查看.ps文件?