Skip to main content

Google Drive

Google Drive allows users to store files in the cloud, synchronize files across devices, and share files.

TagsDriveSaas

Credential configuration

To configure this credential you need to:

  • Enable the Google Drive API for your project in Google Cloud.

  • Obtain the JSON key of a service account. This key is downloaded as a JSON file when you create it. It should look like this:

{
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL",
"universe_domain": "googleapis.com"
}

If you don't have one, create a service account and then, a JSON key for that service account.

info

Make shure to share the root folder with the service account client_email to make this work properly.

Here is an example of a filled credential configuration form in YepCode:

Google Drive Snippets available in editor

note

The title is the triggering text for YepCode to autocomplete the script.

Integration

New integration from credential
const googleDriveClient = await yepcode.integration.googleDrive("credential-slug");
New integration from plain authentication data
const { google } = require("googleapis");

const googleDriveCredentials = {
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL",
"universe_domain": "googleapis.com",
};

const auth = new google.auth.GoogleAuth({
credentials: googleDriveCredentials,
scopes: ['https://www.googleapis.com/auth/drive']
});

const googleDriveClient = google.drive({ version: 'v3', auth });

Find a Folder

Find a folder in 'My Drive' by name
const { data: folder } = await googleDriveClient.files.list({
q: `name='folderName' and mimeType='application/vnd.google-apps.folder'`,
});
console.log(folder.files[0]);
Find a folder in 'Shared Drives' by name
const { data: folder } = await googleDriveClient.files.list({
q: `name='folderName' and mimeType='application/vnd.google-apps.folder'`,
corpora: "drive",
driveId: sharedDrivesRootFolderId, // The sharedDrivesRootFolderId can be obtained in the root folder URL "https://drive.google.com/drive/u/2/folders/---sharedDrivesRootFolderId---"
includeItemsFromAllDrives: true,
supportsAllDrives: true,
});
console.log(folder.files[0]);

Create a Subfolder

Create a subfolder
const { data: subfolder } = await googleDriveClient.files.create({
resource: {
name: "folderName",
mimeType: "application/vnd.google-apps.folder",
parents: ["folderId1", "folderId2"], // You can obtain folderId finding the folder first (seen in the previous examples) and getting the property id (folder.files[0].id)
}
});
console.log(subfolder);

Upload a File to a Folder

Upload a file to a folder
//First we need to create a Readable stream from a Base 64 file content
const { Readable } = require('stream');
const readableStreamFile = Readable.from(Buffer.from(base64FileContent, 'base64'));

const { data: file } = await googleDriveClient.files.create({
media: {
mimeType: "application/octet-stream",
body: readableStreamFile,
},
resource: {
name: "fileName",
parents: ["folderId1", "folderId2"], // You can obtain folderId finding the folder first (seen in the previous examples) and getting the property id (folder.files[0].id)
},
});
console.log(file);