Skip to content

Instantly share code, notes, and snippets.

@Musinux
Created January 17, 2018 00:00
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Musinux/9945da1a2afd284cef5ec0377f4b2460 to your computer and use it in GitHub Desktop.
nodejs drive api v3 fetch children files from folder
const GoogleAuth = require('google-auth-library')
const OAuth2Client = GoogleAuth.OAuth2Client
const google = require('googleapis')
const clientId = '<YOUR_CLIENT_ID>'
const clientSecret = '<YOUR_CLIENT_SECRET>'
const redirectUri = '<Your URI Callback>'
const oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUri)
// your oauth method, see documentation
const drive = google.drive({ version: 'v3', auth: oauth2Client })
/**
From now on, if you want to get the list of children, you must use a query string, in the form of
"'IDofYourFolder in' parents" where "in parents" indicates that Drive should look into IDofYouFolder
**/
drive.files.list({
q: `'${googleFolderId}' in parents`
}, (err, data) => {
if (err) throw err
console.log('your files', data)
})
@iamendy
Copy link

iamendy commented May 28, 2019

I had almost pulled my hair out!! Been searching through google API for hours!!
Thanks for sharing this.

Google drive API is Ugly!!

@ssatyamchauhan
Copy link

You should also mention how clientId, clientSecret and redirecturl can we get.

@Tiffceet
Copy link

Thank you so much ! Didnt knew that we could use "q" to like that.

Man, it could have been nice if the documentation actually mentions how to use the "q" parameter

Peace

@Calvin087
Copy link

Thanks for this, Sorry I've been smashing my head against the desk for two hours.
I can't figure out how to return the value instead of just logging it.

I've tried wrapping it in an async function, but nothing is returning.

@Musinux
Copy link
Author

Musinux commented Apr 4, 2021

You can write something like:

function getChildren (id) {
  return new Promise((resolve, reject) => {
    drive.files.list({ q: `'${id}' in parents` }, (err, data) => {
       if (err) return reject(err)
       console.log('your files', data)
       resolve(data)
    })
  })
}

@Calvin087
Copy link

@Musinux, you've just saved my computer from getting thrown out the window.
What is this technique / pattern / concept called so that I can thoroughly research it?

The resolve thing is new to me (because I'm a noobie)

Thank you.

@Musinux
Copy link
Author

Musinux commented Apr 5, 2021

@Calvin087 it's named Promises, so you can look here and there for some explanations and examples. If you need some links to learn node.js, go to my own list on github you may find some interesting information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment