2016-02-29 179 views
1

我正在使用谷歌驱动器v3 api上传文件,然后在浏览器中使用Web浏览器在响应中预览链接。但是,网络视图链接将变为空。当我使用v2时,我可以使用备用链接进行操作。使用Google Drive V3 API和服务帐户验证,WebViewLink为空

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

我的问题是我怎么能打开使用System.Diagnostics.Process.Start(newFile.WebViewLink);

这里在浏览器中的文件是我的代码:

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

      Permission toShare = new Permission(); 
      toShare.EmailAddress = "[email protected]"; 
      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; 
     } 
    } 

任何人都可以请指导我在这?

+1

http://stackoverflow.com/questions/35317660/google-drive-api-ver3-how-share-on-my的可能重复-web-page – abielita

+0

谢谢@abielita – Atihska

回答

4

只是想在C#中发布上述语法。从谷歌文档,它说我们必须做一个文件,然后请求使用字段属性。 “获得在谷歌驱动器V3 API for .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; 
         } 
       } 
     } 
相关问题