Code Monkey home page Code Monkey logo

node-google-drive's Introduction

node-google-drive

Travis CI FOSSA Status

Library to operate with Google Drive API v3 from Node.js, using system user tokens or personal keys

This library is heavily inspired on theozero's' google-spreadsheet. No, I mean, really inspired. As in blatantly copied. The only difference being his library is to operate with google spreadsheets and this one is to interact with google drive.

So, basically, you can operate in two ways. You either use Google Oauth and manually enter your credentials everytime, or use a Service Account and forget about further authentications.

How to create a Service Account

(the following is taken from google-spreadsheet docs)

  1. Go to the Google Developers Console
  2. Select your project or create a new one (and then select it)
  3. Enable the Drive API for your project
  • In the sidebar on the left, expand APIs & auth > APIs
  • Search for "drive"
  • Click on "Drive API"
  • click the blue "Enable API" button
  1. Create a service account for your project
  • In the sidebar on the left, expand APIs & auth > Credentials
  • Click blue "Add credentials" button
  • Select the "Service account" option
  • Select "Furnish a new private key" checkbox
  • Select the "JSON" key type option
  • Click blue "Create" button
  • your JSON key file is generated and downloaded to your machine (it is the only copy!)
  • note your service account's email address (also available in the JSON key file)
  1. Share the doc (or docs) with your service account using the email noted above

Example usage:

Let's say you stored your user credentials in a file called my_credentials.json. And you gave permission to the service account's email address over a folder in your Google Drive whose id is 1bibD4HDZVbqOPq882YSDTmZlI06fZvLU. So you would do:

const YOUR_ROOT_FOLDER = '1bibD4HDZVbqOPq882YSDTmZlI06fZvLU',
	PATH_TO_CREDENTIALS = path.resolve(`${__dirname}/my_credentials.json`);

// Let's wrap everything in an async function to use await sugar
async function ExampleOperations() {
	const creds_service_user = require(PATH_TO_CREDENTIALS);

	const googleDriveInstance = new NodeGoogleDrive({
		ROOT_FOLDER: YOUR_ROOT_FOLDER
	});

	let gdrive = await googleDriveInstance.useServiceAccountAuth(
		creds_service_user
	);

	// List Folders under the root folder
	let folderResponse = await googleDriveInstance.listFolders(
		YOUR_ROOT_FOLDER,
		null,
		false
	);

	console.log({ folders: folderResponse.folders });

	// Create a folder under your root folder
	let newFolder = { name: 'folder_example' + Date.now() },
		createFolderResponse = await googleDriveInstance.createFolder(
			YOUR_ROOT_FOLDER,
			newFolder.name
		);

	newFolder.id = createFolderResponse.id;

	debug(`Created folder ${newFolder.name} with id ${newFolder.id}`);

	// List files under your root folder.
	let listFilesResponse = await googleDriveInstance.listFiles(
		YOUR_ROOT_FOLDER,
		null,
		false
	);

	for (let file of listFilesResponse.files) {
		debug({ file });
	}
}

ExampleOperations();

See API for a description of available methods.

License

FOSSA Status

node-google-drive's People

Contributors

ffflabs avatar kkbhav avatar cprzytocki avatar zewa666 avatar fossabot avatar gmotzespina avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.