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

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

我正在使用 google drive v3 api 上传文件,然后使用响应中的 WebView 链接在浏览器中预览它。但是 WebView 链接即将变为空。当我使用 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# 中发布上面的语法。从谷歌文档中,它说我们必须获取文件,然后使用 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;
                      }
                 }
         }

关于c# - 使用 Google Drive V3 API 和服务帐户身份验证,WebViewLink 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711489/

相关文章:

c# - 无法找到请求的.Net Framework数据提供程序+ Entity Framework

c# - .NET Core 控制台应用程序的 ASP.NET Core 配置

c# - 无法删除.NET API发布中的文件

.net - dot net maui 图像控件不显示远程源

node.js - 使用 Google Drive API 和 async/await 获取文件缓冲区

python - 使用 python api 显示谷歌驱动器中的子文件夹

c# - Visual Studio 2017 csc.exe 退出,代码为 -2146232797

c# - ASP.NET MVC 需要两个不同版本的 Newtonsoft.Json.dll

.net - IronRuby - .NET 4.0 - 方法名称末尾的问号和感叹号

ios - 如何在 Google 云端硬盘上保存和检索图像