Skip to content

Instantly share code, notes, and snippets.

@Musinux
Created January 17, 2018 00:00
Show Gist options
  • Save Musinux/9945da1a2afd284cef5ec0377f4b2460 to your computer and use it in GitHub Desktop.
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)
})
@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