简体   繁体   中英

Searching file to download in google drive c#

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 checkfile 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM