claOnline
claOnline

Reputation: 1045

How To Use Google Drive API Without OAuth Using Nodejs?

After searching stackoverflow, blogs, YouTube, Google Drive API doc, e.t.c, most of the examples show how to use the Drive API using OAuth.

I want to build a Nodejs application where the Nodejs Server will create Spreadsheets on Google Drive only when new users create an account in my application. The spreadsheet will then be available to the application admin.

This is a server side process so there is no need for the OAuth Consent screen e.t.c

Is there not a way to use the Drive API with just API keys and REST URL’s

The Google Doc link below has examples of using just REST URL’s to interact with the Drive API.

https://developers.google.com/drive/api/v3/reference/files/create

The Google Docs is vague on how to use the Drive API with just API keys and REST URL’s in the link above as the examples use REST URL’s to create files e.t.c

Upvotes: 4

Views: 2228

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

The first thing you need to understand the diffrence between private data and public data.

Public data is data that is not owned by anyone. A good example would be Public youtube videos. You do not need the permission of the owner to see thise videos you can use a Public api key to access the search videos list method and you will be able to access them.

Private data is data that is owned by a user. Your google drive account is private user data. Only you can access that. Only you can grant an application permission to access it.

Public api keys can only bue used to access public data. To access private user data you need the consent of the owner

You want to use the file.create method if you check the documentation you will find that it tells you exactly that.

enter image description here

You need to be authorized and the user must have consented to at least one of those scopes for you to be able to use that method.

So to answer your question Is there not a way to use the Drive API with just API keys and REST URL’s the answer is No there is not. Not with an api key.

However I have an alternative. Your application will be connecting to an account you the developer control. This means that you can use a service account. Service accounts are like dummy users if configured properly you can grant it access to a folder on your drive account by sharing that folder with the service account like you would any other user. Once that is done the service account will be able to access that drive account from your server with no other interaction on your part.

// service account key file from Google Cloud console.
const KEYFILEPATH = 'C:\\Youtube\\dev\\ServiceAccountCred.json';

// Request full drive access.
const SCOPES = ['https://www.googleapis.com/auth/drive'];

// Request full drive scope and profile scope, giving full access to google drive as well as the users basic profile information.
const SCOPES = ['https://www.googleapis.com/auth/drive', 'profile'];
// Create a service account initialize with the service account key file and scope needed
const auth = new google.auth.GoogleAuth({
    keyFile: KEYFILEPATH,
    scopes: SCOPES
});
const driveService = google.drive({version: 'v3', auth});
let fileMetadata = {
        'name': 'icon.png',
        'parents':  [  '10krlloIS2i_2u_ewkdv3_1NqcpmWSL1w'  ]
    };
let response = await driveService.files.create({
    resource: fileMetadata,
    media: media,
    fields: 'id'
});
switch(response.status){
    case 200:
        let file = response.result;
        console.log('Created File Id: ', response.data.id);
        break;
    default:
        console.error('Error creating the file, ' + response.errors);
        break;
}

Code shamelessly copied from Upload Image to Google drive with Node Js

Upvotes: 5

Related Questions