Skip to main content

Google Drive API Javascript Example

On this tutorial, we gonna use Google Drive API for Javascript. We use NodeJS on this tutorial, so you must have NodeJS installed in your PC/Laptop.

For the API authentication, go to console.developers.google.com and choose your option. You can authenticate with API key, or by Oauth 2.0, and by Service Account. Your authentication is almost ready, you have to enable Google API you want to use, by visiting Google API Library https://console.developers.google.com/apis/library and enable Google Drive API.

Follow this step to use Google Drive API.


1. Share Files


To use the service account method of authentication, you’ll need to provide access to the service account. You can provide access on a file-by-file basis or at the level of folders. Find the "client_email" key in your credientials.json file and copy the value. This is the email address that you’ll need to share, and it will look something like:

service-account@projectname-123456.iam.gserviceaccount.com

Create a new folder in your account’s Google Drive, and then share it with the "client-email" in your credentials.json . Sharing access to the service account is required, and it’s one of the reasons a service account is more secure than a simple API key.


2. Install Google API packages


Open the terminal, making sure you’re in the project directory, and type npm i googleapis . The conventional way to import the module into a Node.js file is like this:

const { google } = require('googleapis');

3. Start Coding

Load the Google API library, read your credential.json files and use Google Drive scopes. We'll using Google Auth with JWT for this tutorial:

const { google } = require('googleapis');
const credentials = require('./credentials.json');
const scopes = [ 'https://www.googleapis.com/auth/drive' ];
const auth = new google.auth.JWT( credentials.client_email, null, credentials.private_key, scopes );

test your code with listing all files from your Drive account:

drive.files.list({}, (err, res) => {
if (err) throw err;
const files = res.data.files;
if (files.length) {
files.map((file) => {
console.log(file);
});
} else {
console.log('No files found');
}
});

Comments

Popular posts from this blog

How to Install Traefik in Debian (Without Docker)

 Download the suitable version of Traefik for your system from here: https://github.com/traefik/traefik/releases as an example, we'll be download the linux 64 bit version: wget https://github.com/traefik/traefik/releases/download/v2.8.7/traefik_v2.8.7_linux_amd64.tar.gz extract those package: tar -xzvf traefik_v2.8.7_linux_amd64.tar.gz set the traefik as executable and move the traefik binary to linux bin folder

Create AdminLTE Dashboard with NextJS

Sure, you can create an AdminLTE dashboard in a Next.js project using TypeScript. Here's a step-by-step guide:  Create a Next.js Project with TypeScript:  If you haven't already, create a new Next.js project with TypeScript by running the following commands: npx create-next-app my-adminlte-dashboard --use-npm --typescript cd my-adminlte-dashboard Install Dependencies: 

Cara Disable Antimalware Service Executable di Windows 10

Disadari atau tidak, Windows 10 (dan juga windows-windows lainnya) hadir dengan banyak sekali aplikasi bloatware (aplikasi yang tidak perlu-perlu amat dimiliki oleh end user). Contohnya, adalah aplikasi yang seharusnya sudah tergantikan fungsinya oleh antivirus, seperti Antimalware Service Executable . Aplikasi ini dicurigai membuat Windows 10 mengalami inefisiensi memori/RAM, memakan resource yang tinggi, dengan Load yang tinggi (tanpa limit terkadang). Nah, berikut adalah cara men-disable nya: Tekan tombol Windows + I untuk membuka apliaksi Windows Setting. Pilih icon menu Update and Security Pilih lagi menu disamping kiri Windows Security Pada jendela baru yang muncul, ada pilihan Virus & Threat protection Klik ini Lalu matikan proses Real-time protection tersebut. Dengan Regedit. Buka dialog regedit, Windows + R dan ketik ‘regedit’ Cari Folder regedit ini HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender Buat sebuah DWORD baru dengan klik kanan