Searching file to download in google drive c#

杀马特。学长 韩版系。学妹 提交于 2019-12-11 08:46:05

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/39651993/searching-file-to-download-in-google-drive-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!