Skip to content

Instantly share code, notes, and snippets.

@mulhoon
Last active July 12, 2023 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mulhoon/7fb34a7a5fa489297be7512425ccc046 to your computer and use it in GitHub Desktop.
Save mulhoon/7fb34a7a5fa489297be7512425ccc046 to your computer and use it in GitHub Desktop.
Calendars
const fs = require('fs')
const readline = require('readline')
const { google } = require('googleapis')
const client_secret = require('./client_secret.json')
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = './google-calendar-token.json'
// Get these from https://console.cloud.google.com/apis/credentials
const credentials = client_secret
function authorize(callback) {
const { client_secret, client_id, redirect_uris } = credentials.installed
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
)
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback)
oAuth2Client.setCredentials(token)
callback(oAuth2Client)
})
return oAuth2Client
}
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
})
console.log('Authorize this app by visiting this url: ', authUrl)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('Enter the code from that page here: ', (code) => {
rl.close()
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err)
oAuth2Client.setCredentials(token)
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err)
console.log('Token stored to', TOKEN_PATH)
console.log('ALL DONE!')
})
callback(oAuth2Client)
})
})
}
;(async () => {
authorize(() => {
console.log('Authorized')
})
})()
const { google } = require('googleapis')
const parse = require('./parse')
let token
if (process.env.access_token) {
token = {
access_token: process.env.access_token,
refresh_token: process.env.refresh_token,
token_type: process.env.token_type,
scope: process.env.scope,
expiry_date: process.env.expiry_date,
}
} else {
token = require('./google-calendar-token.json')
}
const credentials = {
installed: {
///
},
}
let calendar
function authorize(callback) {
const { client_secret, client_id, redirect_uris } = credentials.installed
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
)
// console.log(oAuth2Client)
oAuth2Client.setCredentials(token)
calendar = google.calendar({ version: 'v3', auth: oAuth2Client })
callback(oAuth2Client)
}
authorize(() => {
console.log('Authorized google calendar')
console.log('Ready for api calls')
})
const listEvents = async ({ start, end, calendarId }) => {
const day = 8.64e7
const today = Math.floor(new Date().getTime() / day) * day
start = start || today
end = end || today + day
// console.log(start, end)
let timeMax = new Date(end)
let timeMin = new Date(start)
let events = await calendar.events.list({
calendarId,
timeMin,
timeMax,
maxResults: 2500,
singleEvents: true,
orderBy: 'startTime',
})
return events.data.items
}
const get = async ({ calendarId, start, end }) => {
let events = await listEvents({
start,
end,
calendarId,
})
let parsed = parse(events, calendarId)
return parsed
}
module.exports = { get }
// ;(async () => {
// let result = await listEvents({})
// console.log(result)
// })()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment