【问题标题】:Using Google Drive V3 API and service account auth, WebViewLink is null使用 Google Drive V3 API 和服务帐户身份验证,WebViewLink 为空
【发布时间】:2016-02-29 23:01:35
【问题描述】:

我正在使用 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 
}

这里是上传代码:

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

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

【问题讨论】:

标签: c# .net google-drive-api service-accounts


【解决方案1】:

只是想在 C# 中发布上述语法。从谷歌文档中,它说我们必须对文件进行获取,然后使用 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;
                      }
                 }
         }

【讨论】:

  • 谢谢,这工作正常。但是,必须有一种更直接的方式来发布对单个文件的请求,而不是下载整个列表。类似于: FilesResource.GetRequest fileRequest = service.Files.Get(Id);然后继续定义必要的字段。到目前为止我做不到。
猜你喜欢
  • 2013-09-23
  • 1970-01-01
  • 2017-03-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 2014-01-09
  • 1970-01-01
相关资源
最近更新 更多