使用Google Drive V3 API和服务帐户身份验证,WebViewLink为空

时间:2021-08-24 15:16:03

I am using google drive v3 api to upload a file and then preview it in browser using web view link in the response. But web view link is coming null. When i was using v2, I was able to do it using alternate link.

我正在使用google drive v3 api上传文件,然后在浏览器中使用响应中的Web视图链接进行预览。但是Web视图链接无效。当我使用v2时,我能够使用备用链接来完成。

I have not set the parent ref so I am assuming as per the documentation, the file is stored in my drive folder(root) of service account. As I couldn't login to service account, so I shared the file with my existing test gmail account and it was shared.

我没有设置父参考,所以我假设按照文档,文件存储在我的服务帐户的驱动器文件夹(根目录)中。由于我无法登录服务帐户,因此我使用现有的测试gmail帐户共享该文件并将其共享。

My question is how can I open the file in browser using System.Diagnostics.Process.Start(newFile.WebViewLink);

我的问题是如何使用System.Diagnostics.Process.Start(newFile.WebViewLink)在浏览器中打开文件;

here is my code:

这是我的代码:

{
File fileInGoogleDrive = Utils.uploadToDrive(service, pathOfTheFileToBeUploaded, "root");

            Permission toShare = new Permission();
            toShare.EmailAddress = "xyz@gmail.com";
            toShare.Type = "user";
            toShare.Role = "reader";

            PermissionsResource.CreateRequest createRequest = service.Permissions.Create(toShare, fileInGoogleDrive.Id);
            createRequest.Execute();

            return fileInGoogleDrive.WebViewLink; //THIS IS NULL 
}

here is the upload code:

这是上传代码:

public static File uploadToDrive(DriveService _service, string _uploadFile, string _parent = "root")
        {            

            if (!String.IsNullOrEmpty(_uploadFile))
            {     

            File fileMetadata = new File();
            fileMetadata.Name = System.IO.Path.GetFileName(_uploadFile);
            fileMetadata.MimeType = GetMimeType(_uploadFile);                    
            //fileMetadata.Parents = new List<FilesResource>() { new FilesResource() {}};                    

            try
            {
                byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                FilesResource.CreateMediaUpload request = _service.Files.Create(fileMetadata, stream, GetMimeType(_uploadFile));
                request.Upload();
                return request.ResponseBody;    

            }
            catch (System.IO.IOException iox)
            {
                // Log
                return null;
            }
            catch (Exception e) // any special google drive exceptions??
            {
                //Log
                return null;
            }
        }
        else
        {
            //Log file does not exist
            return null;
        }
    }

Could anyone please guide me here?

有人可以指导我吗?

1 个解决方案

#1


4  

Just wanted to post the syntax in C# for the above. From the google documentation, it says we have to do a get on files and then request using Fields property. "Getting the fields in google drive v3 api for .net"

只是想在C#中发布上面的语法。从谷歌文档,它说我们必须做一个get文件,然后请求使用Fields属性。 “在google drive v3 api中获取.net的字段”

  File resultFile = null;
  FilesResource.ListRequest listRequest = _service.Files.List();
    /* Specify camelCase format to specify fields. You can also check in debug mode the files properties before requesting which will be null. All properties will be capitalized so make th efirst letter as small(camel case standard)*/

  listRequest.Fields = "files(id, webViewLink, size)";                
  var files = listRequest.Execute().Files;


        if (files != null && files.Count > 0)
        {
                foreach (var file in files)
                {
                      if (file.Id == _fileId)
                      {
                           Console.WriteLine("{0}, {1}, {2}", file.Id, file.WebViewLink, file.Size);
                           resultFile = file;
                      }
                 }
         }

#1


4  

Just wanted to post the syntax in C# for the above. From the google documentation, it says we have to do a get on files and then request using Fields property. "Getting the fields in google drive v3 api for .net"

只是想在C#中发布上面的语法。从谷歌文档,它说我们必须做一个get文件,然后请求使用Fields属性。 “在google drive v3 api中获取.net的字段”

  File resultFile = null;
  FilesResource.ListRequest listRequest = _service.Files.List();
    /* Specify camelCase format to specify fields. You can also check in debug mode the files properties before requesting which will be null. All properties will be capitalized so make th efirst letter as small(camel case standard)*/

  listRequest.Fields = "files(id, webViewLink, size)";                
  var files = listRequest.Execute().Files;


        if (files != null && files.Count > 0)
        {
                foreach (var file in files)
                {
                      if (file.Id == _fileId)
                      {
                           Console.WriteLine("{0}, {1}, {2}", file.Id, file.WebViewLink, file.Size);
                           resultFile = file;
                      }
                 }
         }