Last active
January 13, 2026 18:38
-
-
Save CodeF53/dde9eb7587d59569589ed9b9c5cb264b to your computer and use it in GitHub Desktop.
Minimal Google Calendar API event fetching JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const client_id = '...' | |
| const client_secret = '...' | |
| const authorizationCodeURL = `https://accounts.google.com/o/oauth2/v2/auth?${new URLSearchParams({ | |
| scope: 'https://www.googleapis.com/auth/calendar', | |
| response_type: 'code', | |
| redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', | |
| client_id, | |
| })}` | |
| const authorizationCode = prompt(`${authorizationCodeURL}\nauth code:`)!.trim() | |
| const token = await fetch('https://www.googleapis.com/oauth2/v4/token', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| client_id, | |
| client_secret, | |
| code: authorizationCode, | |
| grant_type: 'authorization_code', | |
| redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', | |
| }), | |
| }).then(r => r.json()) | |
| const now = Temporal.Now.instant() | |
| const eventResp = await fetch(`https://www.googleapis.com/calendar/v3/calendars/primary/events?${new URLSearchParams({ | |
| timeMin: now.toString(), | |
| timeMax: now.add({ hours: 24 }).toString(), | |
| singleEvents: 'true', // expand reoccurring events into seperate instances | |
| orderBy: 'startTime', | |
| })}`, { | |
| headers: { Authorization: `Bearer ${token.access_token}` }, | |
| }).then(r => r.json()) | |
| const events = eventResp.items | |
| .filter(event => event.status === 'confirmed') // no canceled/tenative | |
| .filter(event => event.start?.dateTime !== undefined) // no all-day events | |
| console.log(events) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment