Created
November 14, 2018 00:21
-
-
Save brizandrew/76820afcc8986865e84f546e93a9e1ff to your computer and use it in GitHub Desktop.
Download a Google Drive file of a given type with a JSON Web Token via Drive Api v3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple Node Script for Downloading Google Drive files (Drive API v3). | |
* Use these instructions to get a GAPI_CLIENT_EMAIL and GAPI_PRIVATE_KEY: https://github.com/The-Politico/api-to-sheets#making-a-google-service-account | |
* See all available types by document here: https://developers.google.com/drive/api/v3/manage-downloads | |
*/ | |
const { JWT } = require('google-auth-library'); | |
const { google } = require('googleapis'); | |
const client = new JWT({ | |
email: GAPI_CLIENT_EMAIL, | |
key: GAPI_PRIVATE_KEY, | |
scopes: ['https://www.googleapis.com/auth/drive'], | |
}); | |
const getData = (fileId, mimeType = 'text/plain') => { | |
const drive = google.drive('v3'); | |
return new Promise((resolve, reject) => { | |
drive.files.export({ | |
auth: client, | |
fileId, | |
mimeType, | |
}, (err, resp) => { | |
if (err) { reject(err); }; | |
resolve(resp.data); | |
}); | |
}); | |
}; | |
client.authorize() | |
.then(() => getData(FILE_ID, FILE_TYPE)) | |
.then(d => { console.log(d); }) // Do something with the file | |
.catch(err => { console.log(err); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment