@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
- Go to the Google Developers Console
- Select your project or create a new one (and then select it)
- 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
- 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)
- 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
- Select Drive API v3 and check all scopes
- Enter this https://www.googleapis.com/auth/drive at the Authorize API form (Dont click the button)
- Click gear icon on the top right corner
- Check Use your own OAuth credentials
- Enter your client_id and client_secret
- Back to Authorize API form and click the button
- Select your gmail account
- Copy response header at the right side
- 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