Navigate back to the homepage

Reading Google Calendar Events in an Azure Function

John Kilmister
July 28th, 2022 · 4 min read

I recently had the need to look at how to read Google Calendar events from inside an Azure function, however it’s not been as straight forward as I initially thought. Having struggled through many different versions of the Google APIs and different technical approaches I settled on the solution which I will share in this post. The Azure functions in this post are written in JavaScript using NodeJS however you should be able to use the same techniques in any supported language.

The technique uses a server-side OAuth 2.0 authorization code flow. This is needed as we require an offline token to use at times when the user is not present.

Navigating to the first Azure function through a link placed on our website the function redirects the user to the google consent page. After consent the user is redirected back to the azure function which lets us obtain the token. We store this token along with an offline refresh token to be later used. The user is then finally redirected back to a webpage on the main website.

The second Azure function retrieves the tokens from key vault. These are then used to access the calendar API reading recent events on a timer.

Setting up your Account

Before we get too far the first task is to configure our Google API access in the Google Cloud Console. From the console create a new project and then select Enable APIs and Services.

Enabling the Calender API

The Enable APIs and Services page allows you to search for APIs to enable. We need to search for the calendar API and select it to be added to our project.

API search page

Now the API has been added we need to configure it. We will start by configuring the OAuth consent screen by selecting External and then on the following screen completing the mandatory contact information.

API Consent page

For reading the calendar API events will just need a single scope /auth/calendar.readonly which we can select from the list. If you need others you can come back and add them at any time.

API scopes

The third and final step is to add test users. Initially while the app is unverified only users in this list will be able to use the app. For testing add your own accounts to this list.

Credentials

Next we need to gather some credentials to use in our application. As we will be using a server-side flow will use a Client ID and Secret.

New client Id

As we create the new Client ID, we are prompted to enter an authorised redirect URL. This is the URL that users will be redirected to after consent is given. For us this will be the URL of our Azure function that handles the authentication flow. If you don’t know the URL at this point you can always come back and add it in later. You can also add in multiple URLs to support both local development and production.

Client Id Details

Make sure you take a note of the ClientID and secret as these will be needed later in our Azure functions.

Authentication Flow Function

The first function we’re going to create is the authentication flow function. We could split this logic into two separate functions however as there is not much code, I have decided to keep it together.

Project Set Up

After creating our JavaScript Azure function, we need to install 3 NPM packages. The first is the Google API, the second is to access key vault where we will be storing our tokens and finally azure identity, to auth with key vault.

1npm install googleapis
2npm install @azure/arm-keyvault
3npm install @azure/identity

The Function Code

1const { google } = require("googleapis");
2const { DefaultAzureCredential } = require("@azure/identity");
3const { SecretClient } = require("@azure/keyvault-secrets");
4
5module.exports = async function (context, req) {
6
7 //Create the google OAuthClient
8 const oauth2Client = new google.auth.OAuth2(
9 process.env["ClientID"],
10 process.env["ClientSecret"],
11 process.env["AuthFunctionUrl"]
12 );
13
14 let nextRedirectUrl;
15
16 if(!req.query.code)
17 {
18 //When we don't have a code must be first request
19 const scopes = ["https://www.googleapis.com/auth/calendar.readonly"];
20
21 nextRedirectUrl = oauth2Client.generateAuthUrl({
22
23 //offline access allows us to get a refresh_token that we can use later
24 access_type: "offline",
25 scope: scopes,
26 include_granted_scopes: true,
27 //forces a refresh token every time
28 prompt: "consent",
29 });
30
31 }else{
32
33 //now we have returned get access and refresh tokens
34 let { tokens } = await oauth2Client.getToken(req.query.code);
35
36 //Save tokens in KeyVault (in real world you want unique keys per user)
37 const credential = new DefaultAzureCredential();
38 const url = `https://${process.env["KeyVaultName"]}.vault.azure.net`;
39
40 const client = new SecretClient(url, credential);
41
42 await client.setSecret("refreshToken", tokens.refresh_token);
43 await client.setSecret("accessToken", tokens.access_token);
44
45 //oauth2Client.setCredentials(tokens);
46 nextRedirectUrl = process.env["SuccessRedirectUrl"]
47 }
48
49 context.res = {
50 status: 302,
51 headers: { "location": nextRedirectUrl },
52 body: null,
53 };
54};

The azure function starts by setting up the Google OAuth client with the Client ID and Secret we gained during ClientID registration. The return URL (AuthFunctionUrl) must match both the functions URL and the URL that we entered in the Google API console.

If the function is accessed without a code query string parameter, we assume that we need to redirect to the consent screen so build up a URL and redirect to it.

Once the user has consented, they will once again be redirected back to this Azure function. This time a code query string parameter will be present and we fall into the else section of the function. Now that we have a code, we can convert it into a set of tokens. With the two tokens we need to store them for use in the next Azure function while we read the calendar items.

In this sample I have decided to store the tokens in key vault although you could choose another storage mechanism. If you are using key vault then create a new key vault resource and place the name in the local.setting.json. For authentication to key vault, I am using the Default Azure Credentials. This means if we have logged in to the Azure CLI then we will also be authenticated with the key vault. In a production environment or when hosted in Azure we could then we can use a manage identity.

Finally, after storing the tokens, we redirect the user back to the main website.

Timer Function

Now we have a token we can access the user’s calendar. For this example, I have decided to do this on a timer trigger to list out the next 10 events coming up. You could of course use any of the APIs that you have been granted consent for.

In the function we start by once again creating the Google OAuth client providing the same Client ID, Secret and return URL. We then read our previously saved refresh and access tokens from key vault. Once we have our tokens, we can set these on the OAuth Client to allow access to the APIs. The client is built so if the access token has expired it will automatically use the refresh token to update. When these are updated, we are notified and can resave them back to key vault.

Finally, we are ready to use the Google Calendar API. This section of the code comes directly from the Google calendars API quick start guide using the OAuth token it reads the next 10 events.

1const { DefaultAzureCredential } = require("@azure/identity");
2const { SecretClient } = require("@azure/keyvault-secrets");
3const { google } = require("googleapis");
4
5module.exports = async function (context, myTimer) {
6
7 //Create the google OAuthClient
8 const oauth2Client = new google.auth.OAuth2(
9 process.env["ClientID"],
10 process.env["ClientSecret"],
11 process.env["AuthFunctionUrl"]
12 );
13
14 //Read tokens in KeyVault (in real world you want unique keys per user)
15 const credential = new DefaultAzureCredential();
16 const url = `https://${process.env["KeyVaultName"]}.vault.azure.net`;
17
18 const client = new SecretClient(url, credential);
19 const refreshToken = (await client.getSecret("refreshToken")).value;
20 const accessToken = (await client.getSecret("accessToken")).value;
21
22
23 oauth2Client.setCredentials({
24 refresh_token: refreshToken,
25 access_token: accessToken
26 });
27
28 //Once the client has a refresh token, access tokens will be acquired
29 //and refreshed automatically in the next call to the API.
30 //If the application has a status of 'Testing' and the consent screen is
31 //configured for an external user type, causing the token to expire in 7 days
32 oauth2Client.on('tokens', (tokens) => {
33 if (tokens.refresh_token) {
34 client.setSecret("refreshToken", tokens.refresh_token);
35 client.setSecret("accessToken", tokens.access_token);
36 console.log("Refresh token updated");
37 }
38 console.log(tokens.access_token);
39 access_token = tokens.access_token;
40 });
41
42 //Now read items in the first calender
43 //code from https://developers.google.com/calendar/api/quickstart/nodejs
44 const calendar = google.calendar({version: 'v3', auth: oauth2Client});
45 calendar.events.list({
46 calendarId: 'primary',
47 timeMin: (new Date()).toISOString(),
48 maxResults: 10,
49 singleEvents: true,
50 orderBy: 'startTime',
51 }, (err, res) => {
52 if (err) return console.log('The API returned an error: ' + err);
53 const events = res.data.items;
54 if (events.length) {
55 console.log('Upcoming 10 events:');
56 events.map((event, i) => {
57 const start = event.start.dateTime || event.start.date;
58 console.log(`${start} - ${event.summary}`);
59 });
60 } else {
61 console.log('No upcoming events found.');
62 }
63 });
64};

Summary

Reading the Google Calendar API after gaining consent from the user can be done using only Azure functions. In this post we looked at how we can initially generate a redirect URL handle the response and then finally use the tokens to read calendar entries.

Title Photo by Jon Tyson on Unsplash

If you enjoyed reading please share...

More articles from John kilmister

Creating Scheduled Reports from Azure Devops with Azure logic apps

Azure DevOps has many features; however it is missing the functionality to send scheduled reports from DevOps. We can fill this gap and…

June 27th, 2022 · 2 min read

An Introduction to Running and Deploying Backstage on Azure

For the last few months, I've had the opportunity to configure and use Backstage with Azure. In this post we will cover what Backstage is…

May 24th, 2022 · 7 min read
Link to $https://twitter.com/johnkilmisterLink to $https://github.com/blueboxesLink to $https://stackoverflow.com/users/33/johnLink to $https://www.linkedin.com/in/johnkilmister/