I am hosting my website on IIS, and using service account to upload and request drive files. I am able to get the webViewLink but because its a service account, it doesn't show me a file on this link but permission issue that service account should grant the permission .
On using code to grant permission for a particular user, then in that user's drive, I am able to see the file. But my question is how can I view file on browser (based on my scenario above) using System.Diagnostics.Process.Start(newFile.webViewLink)
Here is my code for upload and getting the file:
{ File fileInGoogleDrive = Utils.uploadToDrive(service, pathOfTheFileToBeUploaded, "root");
File metadata = Utils.GetFileRequestedMetadata(service, fileInGoogleDrive.Id);
Permission toShare = new Permission();
toShare.EmailAddress = "xyz@gmail.com";
toShare.Type = "user";
toShare.Role = "reader";
PermissionsResource.CreateRequest createRequest = service.Permissions.Create(toShare, metadata.Id);
createRequest.Execute();
return metadata.WebViewLink;
}
public static File uploadToDrive(DriveService _service, string _uploadFile, string _parent = "root")
{
if (!String.IsNullOrEmpty(_uploadFile))
{
//string mimeType = GetMimeType(_uploadFile); // We can have mime type passed in request
File fileMetadata = new File();
fileMetadata.Name = System.IO.Path.GetFileName(_uploadFile);
fileMetadata.MimeType = GetMimeType(_uploadFile);
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;
}