4

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

7
  • 1
    Whos drive account are you uploading the files to one you control or the users? Commented Mar 3, 2022 at 7:55
  • @DaImTo It’s a drive account created for the application. Only Admin has access. Each user will have a folder where their spreadsheets will be created Commented Mar 3, 2022 at 8:04
  • 1
    About 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., in your goal, you want to create a new Spreadsheet to your Google Drive with an API key. Is my understanding correct? For example, will you upload a file? If you want to upload a file, can I ask you about the maximum file size? Commented Mar 3, 2022 at 8:33
  • @Tanaike There is no upload. A user signs up for an account on the application and then a request is sent to Google drive to create a spreadsheet and the spreadsheet ID is added to the users details and then saved in the database. Commented Mar 3, 2022 at 9:51
  • 1
    Thank you for replying. About A user signs up for an account on the application, in this case, the Spreadsheet is created in your Google Drive or a Google Drive you can access as the writer? And, you want to create a new Spreadsheet without the authorization process and with an API key. Is my understanding correct? Commented Mar 3, 2022 at 12:27

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.