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

Ati*_*ska 2 .net c# google-drive-api service-accounts

我正在使用 google drive v3 api 上传文件,然后使用响应中的 web 视图链接在浏览器中预览它。但是网络视图链接即将变为空。当我使用 v2 时,我可以使用备用链接来完成。

我没有设置父引用,所以我假设根据文档,该文件存储在我的服务帐户的驱动器文件夹(根)中。由于我无法登录服务帐户,所以我与我现有的测试 gmail 帐户共享了该文件并共享了该文件。

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

这是我的代码:

{
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 
}
Run Code Online (Sandbox Code Playgroud)

这是上传代码:

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

有人可以在这里指导我吗?

Ati*_*ska 7

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

  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;
                      }
                 }
         }
Run Code Online (Sandbox Code Playgroud)