fix reading credentials

This commit is contained in:
Thomas Ruoff
2022-03-14 13:23:51 +01:00
parent 90fc5c323f
commit 5c293eb973

View File

@@ -1,14 +1,20 @@
import { google } from 'googleapis'
import { Booking } from '../db/booking'
const keyFile = process.env.GOOGLE_SERVICE_ACCOUNT_KEY_FILE
const calendarId = process.env.GOOGLE_CALENDAR_ID
let credentials
try {
credentials = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON);
} catch (error) {
console.error('Unable to parse process.env.GOOGLE_SERVICE_ACCOUNT_KEY_JSON - invalid JSON?');
throw error;
}
const auth = new google.auth.GoogleAuth({
keyFile,
credentials,
scopes: [
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly',
],
})
@@ -39,8 +45,9 @@ function getSummary(booking: Partial<Booking>): string {
}
export async function createCalendarEvent(booking: Booking): Promise<Booking> {
const authInstance = await auth.getClient();
const response = await calendar.events.insert({
auth,
auth: authInstance,
calendarId,
requestBody: {
summary: getSummary(booking),