使用Google Drive V3 API和服务帐户身份验证,WebViewLink为空 [英] Using Google Drive V3 API and service account auth, WebViewLink is null

查看:173
本文介绍了使用Google Drive V3 API和服务帐户身份验证,WebViewLink为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Drive v3 api上传文件,然后在浏览器中使用网页浏览链接在响应中预览它。但是,网络视图链接将变为空。当我使用v2时,我可以使用备用链接进行操作。



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



我的问题是如何在浏览器中使用 System.Diagnostics.Process.Start(newFile.WebViewLink); $ b

这里是我的代码:

  {
文件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();

返回fileInGoogleDrive.WebViewLink; //这是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(){}};

尝试
{
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();
返回request.ResponseBody;

$ b catch(System.IO.IOException iox)
{
// Log
return null;
}
catch(Exception e)//任何特殊的谷歌驱动器异常?
{
// Log
return null;
}
}
else
{
//日志文件不存在
return null;


$ / code>

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

解决方案

只是想在C#中发布上述语法。从谷歌文档,它说我们必须做一个文件,然后请求使用字段属性。
在Google Drive v3 api中获取.net的字段

  File resultFile = null; 
FilesResource.ListRequest listRequest = _service.Files.List();
/ *指定camelCase格式来指定字段。您也可以在调试模式下检查文件属性,然后请求将其为空。所有的属性将被大写,所以使第一个字母小(驼峰标准)* /

listRequest.Fields =files(id,webViewLink,size);
var files = listRequest.Execute()。文件;


if(files!= null&& file.Count> 0)
{
foreach(文件中的var文件)
{
if(file.Id == _fileId)
{
Console.WriteLine({0},{1},{2},file.Id,file.WebViewLink,file)。尺寸);
resultFile = file;
}
}
}


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?

解决方案

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

这篇关于使用Google Drive V3 API和服务帐户身份验证,WebViewLink为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆