Interfacing with Google Cloud Platform (GCP): Connecting Your App to Google’s APIs


Google Cloud Platform (GCP) is Google’s own suite of cloud computing services, similar to Amazon AWS and Microsoft Azure. Often times, you’ll need to interface your client’s or perhaps your own personal applications with any one of Google’s many services like Google Docs, Gmail, or Google Drive.
In this post, we’ll guide you through an example on how to connect your external application (whether the external app is from a custom app or even from another service like AWS Lambda) to Google Sheets using googleapis
client library in a Node.js runtime, but the general authentication blueprint holds for other Google services and programming languages.
1 — Navigate to console.cloud.google.com → You can use a personal google account of yours to login/signup (Note: Personal Google accounts tend to work more reliably than work/organizational accounts for this setup).
2 — Create a new project by selecting the following project picker button on your page (“AWS Sync” is my most recent project, your page will say something else):
3 — Go to the Menu icon at top left -> APIs & Services -> Library, and simply search for and enable the API(s) you need for your project (eg search “google sheets api” to connect with google sheets, etc).
4 — Then go to the Menu icon again -> APIs & Services -> Credentials → Create credentials → Service account. Give it a name and description, keep everything else as-is as it's optional, and click done.
5 — Now go to that service account you just created → keys tab → Add key → Create new key → JSON, then click Create. Your browser should automatically download the JSON file — download it if not. Add its contents to your environment variables (e.g. GOOGLE_SERVICE_ACCOUNT_JSON
).
- E.g. if you're working in a Lambda, simply copy and paste the entire JSON contents into the value of the environment variable. If a Node.js application, add it instead to your
.env
file, etc…
6 — Go to the spreadsheet and:
- Share the
client_email
in that JSON file as an Editor in the Google Sheet you want to export data to.
- Get the spreadsheet’s id from the google sheet URL, and add it to the code (eg
GSHEET_ID
). It comes after the/d/
in the url, eg https://docs.google.com/spreadsheets/d/**<gsheet-id-here>
7 — Now with the following boilerplate code, update the lines below, as needed:
import {google} from 'googleapis';
async function sendToSheets(csv){
const GSHEET_ID = 'your-google-sheet-id'; // update this line
const serviceAccount = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_JSON); // update this line if env variable named differently
const auth = new google.auth.GoogleAuth({
credentials: serviceAccount,
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
const sheets = google.sheets({ version: 'v4', auth });
const sheetName = 'your-existing-sheet-name-here'; // update this line
try {
// To add data to existing tab
const gsheetResponse = await sheets.spreadsheets.values.update({
spreadsheetId: GSHEET_ID,
range: `${sheetName}!A1`, // update this line as needed
valueInputOption: 'RAW',
resource: {
values: [['hello']] // your data here as an array of arrays...
}
});
if (gsheetResponse.status !== 200){
throw new Error('Failed to add data');
}
} catch (error){
console.error("Error: ", error.message);
}
}
Now you are ready to send data to your google spreadsheet!
Summary
That’s it! In this post you learned how to 1) create a service account in GCP in order to authenticate your external application, 2) the very basics of how to use googleapis
and GCP, and 3) how to send data to a Google Spreadsheet. Follow for more!
Subscribe to my newsletter
Read articles from Xavier Reed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
