Using Google Drive V3 API and service account auth

2019-03-01 11:16发布

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.

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.

My question is how can I open the file in browser using 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条回答
三岁会撩人
2楼-- · 2019-03-01 12:13

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"

  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;
                      }
                 }
         }
查看更多
登录 后发表回答