npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@neoxr/google-drive

v1.0.5

Published

Node.js library to access Google Drive's API V3

Downloads

19

Readme

@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