Giter Site home page Giter Site logo

rastogiujj12 / google-drive Goto Github PK

View Code? Open in Web Editor NEW

This project forked from neoxr/google-drive

0.0 0.0 0.0 45 KB

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

License: GNU General Public License v3.0

JavaScript 100.00%

google-drive's Introduction

@neoxr/google-drive

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

How to create a Service Account

  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

How to get Access Token

Go to the OAuth 2.0 Playground

  1. Select Drive API v3 and check all scopes
  2. Enter this https://www.googleapis.com/auth/drive at the Authorize API form (Dont click the button)
  3. Click gear icon on the top right corner
  4. Check Use your own OAuth credentials
  5. Enter your client_id and client_secret
  6. Back to Authorize API form and click the button
  7. Select your gmail account
  8. Copy response header at the right side
  9. Save as token.json

Connect to Google Drive

Let's say you stored your user credentials in a file called credentials.json and token as token.json. And you gave permission to the service account's email address.

const Authorize = require('@neoxr/google-drive')
const drive = new(Authorize('./path/credentials.json', './path/token.json'))

console.log(drive)

Show Account Information

drive.about().then(res => console.log(res))

Check Folder Exists

drive.checkFolderExists('folder_name').then(res => console.log(res))

Create Folder

drive.createFolder('my_new_folder').then(res => console.log(res))

Show File Lists

drive.fileList().then(res => console.log(res))

// filter by mimeType
drive.fileList({
   q: "mimeType='video/mp4'"
}).then(res => console.log(res))

Download File

const fs = require('fs')
drive.getFile('https://drive.google.com/file/d/1hzgrW1rWCvfNmE2CYKov2z8zmzOzSGfq/view?usp=drivesdk').then(res => {
   if (!res.status) {
      console.log('Error!')
   } else {
      fs.writeFileSync(res.data.name, Buffer.from(res.data.chunk))
      console.log('File downloaded!')
   }
})

Upload File

const fs = require('fs')
const Buffer = fs.readFileSync('./path/video.mp4')
// Example Folder : https://drive.google.com/drive/folders/1-hTAMXNpTS0o_RNSKAtUEyJm3fGzwYUy
const folderId = '1-hTAMXNpTS0o_RNSKAtUEyJm3fGzwYUy'
drive.uploadFile(Buffer, folderId).then(res => console.log(res))

Delete File

drive.deleteFile('https://drive.google.com/file/d/1hzgrW1rWCvfNmE2CYKov2z8zmzOzSGfq/view?usp=drivesdk').then(res => console.log(res))

Create Function by Docs

If you want to use the function from Google Drive Docs use drive.GoogleDrive, for example :

const createPermissions = async (drive, fileId) => {
   // Return the Promise result after completing its task
   return new Promise((resolve, reject) => {
      const body = {
         "role": "reader",
         "type": "anyone"
      }
      return drive.GoogleDrive.permissions.create({
         fileId,
         resource: body
      }, (err, results) => err ? reject(err) : resolve(results))
   })
}

Important

You have to put this on the app at 60 minutes intervals to refresh the token.

setInterval(() => {
   drive.refreshAccessToken()
}, 3600000) 

License

Copyright (c) 2022 Neoxr . Licensed under the GNU GPLv3

google-drive's People

Contributors

neoxr avatar rastogiujj12 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.