搜索文件在谷歌驱动器中下载c# [英] Searching file to download in google drive c#

查看:212
本文介绍了搜索文件在谷歌驱动器中下载c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,用于下载我的谷歌驱动器中的图像文件。我能够这样做,但是当我试图搜索一个文件来返回一个特定的文件时,我总是在使用基于本网站 https://developers.google.com/drive/v3/web/search-parameters 。我真的不知道这个问题。这是我的代码

  GoogleHelper gh = new GoogleHelper(); //调用
DriveService服务= GoogleHelper.AuthenticateServiceAccount(电子邮件,securityPath);
列表< String> file = GoogleHelper.GetFiles(service,
mimeType ='image / jpeg',名称包含'aa');
String newFile = newPath + id;
gh.DownloadFile(service,file [0],newPath);
//获取文件方法:
public static List< String> GetFiles(DriveService服务,字符串搜索)
{
List< String> Files = new List< String>();
尝试
{
//列出当前用户的所有文件和目录。
FilesResource.ListRequest list = service.Files.List();
list.MaxResults = 1000;

if(search!= null)
{
list.Q = search;

}

FileList filesFeed = list.Execute();

// MessageBox.Show(filesFeed.Items.Count);
////循环直到我们到达空白页面
while(filesFeed.Items!= null)
{
//将每个项目添加到列表中。
foreach(filesFeed.Items中的文件项)
{
Files.Add(item.Id);

}

//当下一个页面标记为
// null时,我们将知道我们处于最后一页。
//如果是这种情况,请打破。

if(filesFeed.NextPageToken == null)
{
break;
}

//准备下一页结果
list.PageToken = filesFeed.NextPageToken;

//执行并处理下一页请求
filesFeed = list.Execute();



catch(Exception ex)
{
//如果请求发生错误,
Console.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
返回文件;


解决方案

a href =https://developers.google.com/drive/v3/web/search-parameters =nofollow>搜索文件

 名称字符串contains1,=,!=文件的名称。 

他们还显示正在使用

  name包含'hello'并且name包含'goodbye'

现在file.list方法返回一个文件资源列表。如果您查看文件资源,则名称不是参数 title 是。



所以,如果你这样做的话

  mimeType ='image / jpeg'和(title包含'a')

您的要求

现在,文档错误的原因是您正在使用Google Drive v2 API,并且文档显然已经针对Google Drive v3进行了更新,您猜对了使用名称而不是标题作为文件。

IMO应该有两个,因为它只是在这里有不同的API。


I am trying to create a program that will download image files in my google drive. I was able to do so, however when I am trying to search a file to return a specific file I always got an error when using the 'name' field which is base on this website https://developers.google.com/drive/v3/web/search-parameters. I don't really know the problem. This is my code

 GoogleHelper gh = new GoogleHelper();//calling
        DriveService service = GoogleHelper.AuthenticateServiceAccount(email, securityPath);
        List<String> file = GoogleHelper.GetFiles(service, 
"mimeType='image/jpeg' and name contains 'aa'");
        String newFile = newPath+id;
        gh.DownloadFile(service, file[0],newPath);
//get File Method:
    public static List<String> GetFiles(DriveService service, string search)
    {
        List<String> Files = new List<String>();
        try
        {
            //List all of the files and directories for the current user. 
            FilesResource.ListRequest list = service.Files.List();
            list.MaxResults = 1000;

            if (search != null)
            {
                list.Q = search;

            }

            FileList filesFeed = list.Execute();

           // MessageBox.Show(filesFeed.Items.Count);
            //// Loop through until we arrive at an empty page
            while (filesFeed.Items != null)
            {
                // Adding each item  to the list.
                foreach (File item in filesFeed.Items)
                {
                    Files.Add(item.Id);

                }

                // We will know we are on the last page when the next page token is
                // null.
                // If this is the case, break.

                if (filesFeed.NextPageToken == null)
                {
                    break;
                }

                // Prepare the next page of results
                list.PageToken = filesFeed.NextPageToken;

                // Execute and process the next page request
                filesFeed = list.Execute();

            }
        }
        catch (Exception ex)
        {
            // In the event there is an error with the request.
            Console.WriteLine(ex.Message);
            MessageBox.Show(ex.Message);
        }
        return Files;
    }

解决方案

If we check the documentation Search for Files

name    string  contains1, =, !=    Name of the file.

They also show it being used

name contains 'hello' and name contains 'goodbye'

Now the file.list method returns a List of file resources. If you check file resources name is not a parameter title is.

So if you do

mimeType='image/jpeg' and (title contains 'a')

Your request will work.

Now the reason the documentation is wrong is that you are using the Google Drive v2 API and the documentation has apparently been updated for Google Drive v3 which you guessed it uses name instead of title for a file.

IMO there should be two because well its just different APIs here.

这篇关于搜索文件在谷歌驱动器中下载c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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