├── README.md ├── documentation.md ├── driveApi.js ├── listFile.js ├── package.json └── uploadFile.js /README.md: -------------------------------------------------------------------------------- 1 | # Google-Drive-API-Tutorial-in-NodeJs---service-account-settings. 2 | A Google Drive API code in order to communicate with the Google Drive web service. 3 | 4 | Settings : Node.js, service account context. 5 | -------------------------------------------------------------------------------- /documentation.md: -------------------------------------------------------------------------------- 1 | /***** links - websites requirements ******/ 2 | 3 | // Node.js's Javascript runtime 4 | https://nodejs.org/en/download 5 | 6 | // Atom editor 7 | https://atom.io 8 | 9 | // is Google's site for software development tools, 10 | // application programming interfaces (APIs), 11 | // and technical resources 12 | https://console.developers.google.com 13 | 14 | 15 | 16 | 17 | /***** readings - Google API documentation *****/ 18 | 19 | // article which explain 20 | // in plain words the google Apps functionment. 21 | https://medium.com/@abhimanyuPathania/google-drive-service-accounts-and-nodejs-a038b8ec482f 22 | 23 | // scopes documentation 24 | https://developers.google.com/identity/protocols/googlescopes // # Drive API, v3 25 | 26 | // reference parameters for API calls 27 | https://developers.google.com/drive/api/v3/reference 28 | 29 | 30 | // general google API documentation. 31 | https://tanaikech.github.io/post 32 | 33 | 34 | /***** Bonus - some usefull documentations *****/ 35 | 36 | // error handling in Node.js http://thenodeway.io/posts/understanding-error-first-callbacks/?utm_source=fredkschott.com&utm_medium=referral 37 | 38 | // npm scripts as a building tools 39 | https://deliciousbrains.com/npm-build-script/ 40 | 41 | 42 | // npm "run script" command documentation 43 | https://docs.npmjs.com/cli/run-script 44 | -------------------------------------------------------------------------------- /driveApi.js: -------------------------------------------------------------------------------- 1 | /***** import primaries materials in order to build the Api code *****/ 2 | // import Google api library 3 | var { 4 | google 5 | } = require("googleapis"); 6 | // import the Google drive module in google library 7 | var drive = google.drive("v3"); 8 | // import our private key 9 | var key = require("./private_key.json"); 10 | 11 | // import path ° directories calls ° 12 | var path = require("path"); 13 | // import fs ° handle data in the file system ° 14 | var fs = require("fs"); 15 | 16 | 17 | /***** make the request to retrieve an authorization allowing to works 18 | with the Google drive web service *****/ 19 | // retrieve a JWT 20 | var jwToken = new google.auth.JWT( 21 | key.client_email, 22 | null, 23 | key.private_key, ["https://www.googleapis.com/auth/drive"], 24 | null 25 | ); 26 | jwToken.authorize((authErr) => { 27 | if (authErr) { 28 | console.log("error : " + authErr); 29 | return; 30 | } else { 31 | console.log("Authorization accorded"); 32 | } 33 | }); 34 | -------------------------------------------------------------------------------- /listFile.js: -------------------------------------------------------------------------------- 1 | // list file in speciifcs folder 2 | var parents = "enter here the folder you target" 3 | drive.files.list({ 4 | 5 | auth: jwtClient, 6 | pageSize: 10, 7 | q: "'" + parents + "' in parents and trashed=false", 8 | fields: 'files(id, name)', 9 | }, (err, { 10 | data 11 | }) => { 12 | if (err) return console.log('The API returned an error: ' + err); 13 | const files = data.files; 14 | if (files.length) { 15 | console.log('Files:'); 16 | files.map((file) => { 17 | console.log(`${file.name} (${file.id})`); 18 | }); 19 | } else { 20 | console.log('No files found.'); 21 | } 22 | }); 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "googledriveapi", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "googleapis": "^31.0.2" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /uploadFile.js: -------------------------------------------------------------------------------- 1 | // upload file in specific folder 2 | var folderId = "enter here the folder you target"; 3 | var fileMetadata = { 4 | 'name': 'text.txt', 5 | parents: [folderId] 6 | }; 7 | var media = { 8 | mimeType: 'text/plain', 9 | body: fs.createReadStream(path.join(__dirname, './text.txt')) 10 | }; 11 | drive.files.create({ 12 | auth: jwtClient, 13 | resource: fileMetadata, 14 | media: media, 15 | fields: 'id' 16 | }, function(err, file) { 17 | if (err) { 18 | // Handle error 19 | console.error(err); 20 | } else { 21 | console.log('File Id: ', file.id); 22 | } 23 | }); 24 | --------------------------------------------------------------------------------