Unlock the Power of Google APIs with TypeScript

With over 200 APIs at your disposal, Google offers a treasure trove of possibilities for developers. But to tap into this wealth of resources, you need to know how to authenticate with Google using TypeScript.

Creating an OAuth 2.0 Client ID

To begin, create a project on the Google Cloud Platform. The project name isn’t crucial, but it’s helpful to name it according to the API you plan to integrate with. In our case, we’ll focus on the Google Calendar API. Once you’ve created the project, navigate to the credentials screen and create an OAuth Client ID using the Create Credentials dropdown.

OAuth Consent Screen and App Registration

Before creating the OAuth Client ID, you’ll need to set up an OAuth consent screen. This may seem daunting, but you can generally accept the defaults and proceed. You’ll also need to register your app, providing a name and your email address. Don’t worry about scopes; you can plan to publish the app or set yourself up as a test user.

Acquiring a Refresh Token

Now that you have your client ID and client secret, you’re ready to write a simple Node command-line application to obtain a refresh token. This involves a multistage process:

  • Provide the Google authentication provider with the client ID and client secret to get an authentication URL.
  • Open the authentication URL in the browser and grant consent to receive a code.
  • Use the client ID, client secret, and code to acquire a refresh token.

Let’s start coding! We’ll initialize a TypeScript Node project and add the necessary dependencies, including the googleapis package. We’ll create a file called google-api-auth.ts and a common file named shared.ts to reuse later.

npm init -y
npm install googleapis

Getting the Refresh Token

Run the command npm run google-api-auth -- --clientId CLIENT_ID --clientSecret CLIENT_SECRET to generate an authentication URL. Click on the URL, authenticate, and grant consent to receive a code. Then, quickly paste the acquired code into the next command to get the refresh token.

import { google } from 'googleapis';

async function getRefreshToken(clientId: string, clientSecret: string, code: string) {
  const auth = new google.auth.OAuth2(clientId, clientSecret);
  const tokens = await auth.getToken(code);
  const refreshToken = tokens.refresh_token;
  return refreshToken;
}

const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const code = 'AUTH_CODE_FROM_BROWSER';

getRefreshToken(clientId, clientSecret, code).then((refreshToken) => {
  console.log(`Refresh Token: ${refreshToken}`);
});

Accessing the Google Calendar API

Now that you have the refresh token, you can use it to access the Google Calendar API. Create a calendar.ts file and use the client ID, client secret, and refresh token to retrieve the list of calendars.

import { google } from 'googleapis';

async function getCalendars(clientId: string, clientSecret: string, refreshToken: string) {
  const auth = new google.auth.OAuth2(clientId, clientSecret);
  auth.setCredentials({ refresh_token: refreshToken });
  const calendar = google.calendar('v3');
  const response = await calendar.calendarList.list({ auth });
  const calendars = response.data.items;
  return calendars;
}

const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const refreshToken = 'YOUR_REFRESH_TOKEN';

getCalendars(clientId, clientSecret, refreshToken).then((calendars) => {
  console.log(calendars);
});

Integrating with Other Google APIs

We’ve demonstrated how to integrate with the Google Calendar API, but the possibilities don’t stop there. With over 200 APIs at your disposal, you can follow the same authentication steps to integrate with the YouTube API, Gmail API, and many more.

Take your API integration to the next level!

Leave a Reply