Google-api-nodejs-client: sharedWithMe field in files.list method returns Bad Request error 400 when sharedWithMe = false

Created on 28 Apr 2018  路  4Comments  路  Source: googleapis/google-api-nodejs-client

//get all of the files within a given parent (folder) and mimeType
const getFiles = (auth, parent, mimeType) => { 
    return new Promise((resolve, reject) => {
        let request = { //make the request header
            auth: auth,
            corpora: "user",
            spaces: "drive",
            includeTeamDriveItems: false,
            q: "sharedWithMe = false"
        }
        if (parent) { 
        //backticks for template literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
            request.q += ` and '${parent}' in parents` //get all the user's files with the given mimeType
        } 
        if (mimeType) {
            request.q += ` and mimeType = '${mimeType}'` //get all the user's files with the given parent
        }
        drive.files.list(request, (error, response) => { //call the api files.list method https://developers.google.com/drive/v3/reference/files/list
            if (error) {
                reject(error)
            } else {
                let files = {}
                response.data.files.forEach(file => {
                    files[file.name] = file.id
                })
                resolve(files) //return an object with the name and id of the files
            }
        })
    })
}

When calling the function getFiles like so (where auth is my OAuth2Client):

getFiles(auth).then((files) => {
    console.log(files)
})

drives.files.list returns a Bad Request 400 error.

Changing request.q to "sharedWithMe = true" or even "sharedWithMe != false" returns the expected output.

external bug

All 4 comments

Apologies for the trouble! This looks like a bug with the underlying API. I can reproduce this pretty easily here:
https://developers.google.com/drive/v3/reference/files/list

Sadly, that means there isn't much I can do with it. I filed an internal bug in hopes of getting the issue cleared up, and I'll report back here if there are any changes there.

On a side note - you don't have to do all that song and dance for promises! We added native promise support in 28.1.0, so you can just do this:

//get all of the files within a given parent (folder) and mimeType
const getFiles = (auth, parent, mimeType) => { 
  let request = { //make the request header
    auth: auth,
    corpora: "user",
    spaces: "drive",
    includeTeamDriveItems: false,
    q: "sharedWithMe = false"
  }
  if (parent) { 
    request.q += ` and '${parent}' in parents` //get all the user's files with the given mimeType
  } 
  if (mimeType) {
    request.q += ` and mimeType = '${mimeType}'` //get all the user's files with the given parent
  }

  return drive.files.list(request).then(r => {
    let files = {};
    r.data.files.forEach(f => files[f.name] = f.id);
    return files;
  });
}

Hope that helps :)

Thank you Justin for filing that bug and much appreciated for the tip. 馃槃

Greetings! Sadly, I haven't heard back yet. I would suggest filing a bug in the drive specific API tracker:
https://issuetracker.google.com/issues/new?component=191650&template=824106

Since there's nothing I can do here, I'm going to go ahead and close this one out for now. Apologies!

this happens with .net client also, version v3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hainguyents13 picture hainguyents13  路  3Comments

leecheve picture leecheve  路  3Comments

streamnsight picture streamnsight  路  4Comments

lowagner picture lowagner  路  3Comments

ACMerriman picture ACMerriman  路  3Comments