3

I am trying to upload a file to Google Drive to a specific folder. Since I need the ID of the folder (not just the name) to set the upload destination (file's parent) I try to search for it by name and get the first returned file's ID from the query below. Instead of a result I get an error:

Google.Apis.Requests.RequestError Invalid Value [400]
Errors [ Message[Invalid Value] Location[q - parameter] Reason[invalid] Domain[global]
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
...

If I try to search for anything really I either get this error or an empty response (i.e. query for all directories mimeType='application/vnd.google-apps.folder' returns an empty list (doesn't throw an error though)).

The relevant snippet of my code:

FilesResource.ListRequest request = service.Files.List();
request.Q = "title='test_folder'";
string folderId = request.Execute().Files[0].Id;    // Error occurs here upon execution
...
fileMeta.Parents = new List<string> { folderId };

The funny thing is, this exact query works on Google's API test site on API v2, but not API v3. The query to get all of the folders works on both though (v2 and v3 on Google's test site), but I get an empty response via my .NET application.

PS: File upload to the "My Drive" directory works, drive services operations like setting the file permissions work etc.

Open to ideas and suggestions on what to check / where I screwed up.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
alkra_dev
  • 97
  • 2
  • 11

2 Answers2

7

The problem you are having is that you are using Google Drive v3 API its File.Name not File.Title. Title was used in v2.

request.Q = "mimeType = 'application/vnd.google-apps.folder' and name = 'test_folder'";

Its a known bug and I have already reported the bug in the documentation to Google more then a year ago still waiting for them to fix the issue.

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • This indeed did the trick. Apparently I have deeper issues though since every correct v3 query returns with an empty response (I get an empty list back). But thank you for the answer, I will mark it as such either way, but could you point me in a direction - what should I check to make sure my queries work correctly? – alkra_dev Nov 06 '17 at 12:15
  • 1
    do a file.list without the q and see what it returns. Remember if you are using a service accounts its not going to have any files until you upload them. Test stuff here https://developers.google.com/apis-explorer/#p/drive/v3/ – Linda Lawton - DaImTo Nov 06 '17 at 12:16
  • I did specify/allow all of the possible DriveService scopes just so I'd not have to deal with any potential access issues. It seems it only lists the files I have uploaded from the application (even with credentials I used in the past, but alas). I have .Drive, .DriveFile and .DriveMetadata scopes in use currently. What is the next step, what am I missing here? – alkra_dev Nov 06 '17 at 13:13
  • 1
    If you are logged in using Oauth2. They you only have access to the files owned by the user. If you are using a service account you only have access to the files owned by the service account. The scope covers what you are allowed to do with the files that are already on the users account. – Linda Lawton - DaImTo Nov 06 '17 at 14:33
  • I am finally done. Apparently I forgot to delete the .credentials folder after adding the scope .Drive instead of .DriveFile which only allowed me to view files added from my app. Thank you for the assistance. You have helped me resolve my issue quicker than I could've done it myself (probably would've taken me a week before I got up to speed). – alkra_dev Nov 07 '17 at 05:20
0

Change the row:

request.Q = "title='test_folder'";

To

request.Q = "name='test_folder'";

Note: Deleted folders and files still have their IDs (except for permanent deletion - in the recycle bin). so some-case this row will be error or not return true result:

string folderId = request.Execute().Files[0].Id; 

Hope this help.

Datusa
  • 101
  • 1
  • 3