I'm developing a Web Application that is integrated with Google Drive.
The App helps you to create drawings and save them in your own Google Drive - just like Google Docs.
This gist shows you the code that does the integration with Google Drive, and a little about my experience in learning about it and getting it to work.
The App is purely client side code. There is no backend server - which is highly relevant to the integration approach, and for user authentication in particular.
The App is a Single Page Web Application (SPA) that helps you to make drawings. I'm talking about technical drawings a bit like CAD drawings or complex diagrams where you want to position everything exactly.
All the complex business logic and heavy lifting for the App is implemented as a WASM module. (Go code)
But Google integration for user authentication and authorisation, and for interacting with Google Drive is javascript code.
It's still in development. But the integration works - and I've decided to share what I've learned about the integration in its raw state, and while the integration journey is still fresh in my mind.
In what follows:
- I'll provide some reference links
- Provide a bit more orientation narrative
- Signpost the roles of code files I've included in the gist
A prerequisite is setting up a Google Cloud (GCP) project. The quickstart guide referenced guide covers that. When you setup your cloud project it will give you some access credentials - like an API key, and a ClientID. Your javascript will need to use those credentials, but you must not put them directly into your code to preserve their secrecy when your code is running in the browser. You'll see in my code files - that I pull their values from environment variables. You might think environment variables is an odd concept when we're talking about code running in the browser, and you'll need some config in your final deployment config to provide the values in a secure way. I use Vite as my javascript project build tool and bundler, and Vite supports environment variables injected at the bundle build stage. Then, later, if you use Vercel to deploy your App, it can pull secrets you've configured in your Vercel project when it performs your Vite build. That process is document here .
There is a parent, or root Google API - that the quickstart shows how to setup into the javascript global namespace as gapi. I have nothing to add to those instructions. It is a bit convoluted though, because it is a multi-step process and involves the code having a chat with Google to "discover" the APIs you've requested to use in your GCP project, then it downloads the corresponding code. Then you have to put in a line of code to initialise it etc. Part of that is to auto-generate the client javascript SDK for you. It really is "for you" the SDK generated covers only the APIs you've asked for in your project. Then you have to make sure you don't try to call any of the integration code until it is all ready.
You are actually initialising two separate Google services: 1) the Identity Service, 2) the Drive API.
The quickstart guide shows you how to achieve all this in a single body of javascript code - and it probably is the easiest way to get off the ground. But I've found for my real-world project that I needed to split the functionality into separate places to seperate concerns respectably.
Once the APIs are both ready, Google offer two ways to interact with the Drive APIs: 1) using the SDK client, 2) using the REST API just like you would any other rest API.
What they don't mention is that the SDK client doesn't cover all the things available in the REST API. The things missing are very difficult to work out - and I wasted a lot of time trying to work out how to do something with the SDK client that ultimately simply wasn't available. I discovered that when I wanted to create a new file on Google Drive - prepopulated with some content. That forced me to learn how to use the REST API (which you'll see in my code files). In my opinion, using the REST API is much cleaner and easier, and certainly makes debugging very much easier. I went back and changed all my Drive API calls over to the REST pattern.
drivecrudops.js
This is a module that Creates, Reads, Updates, and Deletes (i.e. CRUD operations) files and folders in Google Drive. You'll see that each function expects an access token as its first argument, which allows the module to be concerned ONLY with the CRUD logic, and not have to think about access permissions itself. You'll see how to CALL these functions in drivewrapper.ts below.
drivewrapper.ts
You can't just call the functions in drivecrudops.js.
Instead you have to orchestrate them being called in a way that first obtains a Google Drive access token and only then calls the function - passing in the access token as the first argument.
I won't say more about here - because it is richly commented in the file itself.