Using google sheets as a database authenticating from the code - javascript

I am working on a POC (a Vue based web application) for which I require a database. I want to use the google sheet as a database
I followed the official Google Sheets API v4 documentation and I am able to perform all kinds of database operations on the google sheet. But, it requires the user to explicitly click on a button and sign in to authenticate. I want it to be done from the code itself.
I have the following keys which are required along with the user consent to use the google sheets API
// Client ID and API key from the Developer Console
var CLIENT_ID = '<YOUR_CLIENT_ID>';
var API_KEY = '<YOUR_API_KEY>';
Apart from this I also have a client secret which I have absolutely no idea how to use it in the code. From what I have read over the internet and what I have understood, it is used to authenticate the access to google sheets API from the code. But, I don't know how.
I did come across some of the third-party services like Sheetsu and SheetDB which I don't want to use. So, is there a way I can use google sheets as a database for simple CRUD operations just as we will do with any traditional database?

It is behaving that way - expecting a user to authenticate - because you are using OAuth2 authentication which is meant for scenarios where a user grants your program access to their spreadsheet.
For your scenario you want to use the other type of authentication: service account.
Actually, you can use a service account with OAuth, as described here:
Using OAuth 2.0 for Server to Server Applications
https://developers.google.com/identity/protocols/oauth2/service-account
And you could consider this because the code examples tend to use OAuth, but I'm suggesting that you instead go back to the sheets API in the console:
https://console.cloud.google.com/apis/api/sheets.googleapis.com/credentials?project=yourProjectId
And click 'create credentials' and choose a service account instead of OAuth. This method has a simpler authentication flow since it is meant for your scenario: no end-user.
Also, I haven't done this in a while, but I think I took the service account client_email (the email address of the account - the main identifier) and give it permission to access the sheet, ie. share your sheet with it.
Finally, I would note - and you probably already know this - but you mention using sheets like a database and it doesn't have the performance characteristics for this. It is more like a CRM: eg. suitable for a building a static site.

Related

Google Picker API for Google Workspace users only

Hi, I want the users of our organization only to use google picker to select files from their google drive.
I followed this example to set up API key and oAuth client in the google developer console.
Here is the summary of what I did:
Enabled Google Picker API in Google API Console
Created API Key
Created OAuth client
Using Official Example google picker is working but it allows all Gmail users as well, I want to allow only those emails which belong to my domain.
I could not found anything related to this in official docs so I have the following questions:
Is it possible?
Is it possible to bypass the consent screen using service account impersonation(User Access Token generated on the server via impersonation)?
This is a high level explanation, but the idea is the following:
Using a server side script, you will generate a token for a user using a service account and impersonation.
On the client side, you will have to call that script on the server side and have the server return the token.
When initializing the picker, you will set the token to what you received from the server using the PickerBuilder.setOAuthToken() method.
That way you not only bypass the consent screen, but also make sure the drive picker presents the drive files of the user you authenticated with the service account.
If you are looking for a low level explanation, edit your question and post your architecture along with your code.

Google JavaScript API - need JavaScript (Angular) application to authenticate as admin account

I am attempting to create an angular application that authenticates to the Google APIs as our administrator account so that it can gather details, such as a user's Google Drive info (number of files, total size, etc.)
I'm attempting to use OAuth 2.0 to give the application our "administrator" credentials. The examples provided in the Google API documentation shows how to accomplish such tasks by allowing a user to log themselves in, but I've yet to find anything demonstrating how to programmaticly authenticate an application as admin (or anything else, for that matter).
Is this possible?
After a bit more research, I finally found a question relevant to the issue with what appears to be a detailed solution! It's all about using the right key words... "programatically" was the word that finally got me there :)
Of course, I'll be using JavaScript/Angular, not Python, but I believe the concepts will be the same.

get google authentication token using username and password

I have been looking for an open google api to get google authentication token by using a user's google username and password. I have came across google-auth node package. And according to it, I first need to log in to create a project in google developer console and then use the key to access the information.
But I want to know whether there is any open api where I can directly pass the username and password, without creating any project in google developer console. And get the authentication token as part of response.
This is my first project using google apis, so if it sounds naive please let me know the right way of authenticating a google account user using a node project.
Sorry, I can't provide any code, as I am stuck at understanding the initial part itself, about how to authenticate different users.
Thanks in advance
You can't use google or any social login, without first creating a project and using it's api key.
That is just how oAuth is designed.

How to allow users to access my Google API application without authenticating for it?

I've developed a small webapp, that pulls raw data from a Google Spreadsheet and builds it on a html, so my website users can see, filter and navigate through the data.
For that, I'm using Google Sheets API v4, but even going to an HTML, users are asked to "AUTHORIZE" access using their Google Accounts, before seeing the table/data.
Is there a way to display this table/data on the HTML, without requesting users to authorize through OAuth?
If you're dealing with accessing data and performing Google API calls, authorization is Google's way to protect and secure the user's data.
If you want to display the data without needing to undergo authorization, publish it to the web:
File -> Publish to the web. An embed code which contains a public url will be provided for you.
Here's a sample of my public data:
https://docs.google.com/spreadsheets/d/1mvFPT2Xx8bNEMUdPC9TDOexzNqVS399bxQATV3JVOyg/pubhtml?widget=true&amp

Write to a Google spreadsheet from JavaScript using the Public API access (no OAuth)

I'd like to allow a user to visit one of my sites, enter some information into a field, and then save that information into a Google Spreadsheet via JavaScript.
I don't want the user to login via Google or have to do any special authentication.
It's Ok if the spreadsheet needs to be open to public; the data's not sensitive.
I don't want to use a Google Form, I want to have full control over the client-side UI.
I've been reading through the Google developer docs, but they only make mention to an OAuth login solution. The Google Developer Console allows you to create a "Public API access key", but does not explain how it's used.
All of the examples are for the scenario where the user is using your app to access his own files. In your case, you want the app to access your own file. This isn't easy. The only ways I can see are :-
Use a server app (appengine works well) to do the access
Very carefully set up permissions and store a refresh token in your app.
Option 2 could be your worst security nightmare if you don't do it carefully, and even then may be an infringement of Google T's and C's since it's akin to distributing your password.
Once you figure out the auth, you'll need to check out the spreadsheet API (https://developers.google.com/google-apps/spreadsheets/), as this is the API that allows you to update an existing spreadsheet. The Drive API can only upload an entire new spreadsheet.

Categories