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

时间:2021-11-18 07:25:54

Can i view a file in google drive in browser using C#, i have tried somethings with google api. but always it's asking for login details. How can i view file in browser using c#?

我可以使用C#在浏览器中查看谷歌驱动器中的文件,我已经尝试使用谷歌api。但总是要求登录详细信息。如何使用c#在浏览器中查看文件?

here i have tried,

我在这里试过,

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 = "xxxxxxxxxxx@gmail.com";

        //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);
        }
    }

Please, anyone have any idea regarding this?

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

1 个解决方案

#1


0  

When you share a file with a user using Permissions: create there is an option to send them a notification. This tells them that the file is shared with them.

当您使用权限与用户共享文件时:创建有一个选项可以向他们发送通知。这告诉他们文件是与他们共享的。

Now that the file is shared with them File.get returns a file resource note make sure you add fields= * or you wont get much of the meta data back from file.get.

现在文件与它们共享File.get返回文件资源注释确保添加fields = *或者您不会从file.get获得大量元数据。

webViewLink
A link for opening the file in a relevant Google editor or viewer in a browser.

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

Basically if the user in question has access then you will get this link back to the file. Something like this

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

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

What I haven't been able to figure out is how to create a sharable link you can on the website so I assume there is a way of doing it though the API as well. Currently I think the file has to either be public or the user must have access before you get a webview link back.

我无法弄清楚的是如何在网站上创建一个可共享的链接,所以我假设有一种方法可以通过API进行。目前我认为该文件必须是公共的,或者用户必须具有访问权限才能获得webview链接。

#1


0  

When you share a file with a user using Permissions: create there is an option to send them a notification. This tells them that the file is shared with them.

当您使用权限与用户共享文件时:创建有一个选项可以向他们发送通知。这告诉他们文件是与他们共享的。

Now that the file is shared with them File.get returns a file resource note make sure you add fields= * or you wont get much of the meta data back from file.get.

现在文件与它们共享File.get返回文件资源注释确保添加fields = *或者您不会从file.get获得大量元数据。

webViewLink
A link for opening the file in a relevant Google editor or viewer in a browser.

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

Basically if the user in question has access then you will get this link back to the file. Something like this

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

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

What I haven't been able to figure out is how to create a sharable link you can on the website so I assume there is a way of doing it though the API as well. Currently I think the file has to either be public or the user must have access before you get a webview link back.

我无法弄清楚的是如何在网站上创建一个可共享的链接,所以我假设有一种方法可以通过API进行。目前我认为该文件必须是公共的,或者用户必须具有访问权限才能获得webview链接。