Skip to content

Calendar #736

Description

@ngoiyaeric

Answer

To use Google Calendar with Clerk authentication, you need to enable Google OAuth and request the appropriate calendar scopes. (1)

Required Scopes

When configuring Google as a social connection, include these scopes to access calendar data:

Accessing Calendar Data

Once a user signs in with Google, Clerk stores and manages their OAuth tokens automatically. Use getUserOauthAccessToken to retrieve the access token for making Google Calendar API requests:

'use server'

import { auth, clerkClient } from '@clerk/nextjs/server'

export async function getGoogleToken() {
 const { userId } = await auth()
 const client = await clerkClient()
 const token = await client.users.getUserOauthAccessToken(userId || '', 'oauth_google')
 return {
 token: token.data[0].token,
 }
}

Making Calendar Requests

Use the retrieved token to call the Google Calendar API:

const client = await clerkClient()
const token = await client.users.getUserOauthAccessToken(userId, 'oauth_google')
const accessToken = token.data[0].token

// Code removed for brevity.
const response = await fetch(
 `<https://www.googleapis.com/calendar/v3/calendars/$%7BencodeURIComponent(>
 calendarId,
 ) }/events?timeMin=${monthStart.toISOString() }&timeMax=${monthEnd.toISOString() }&singleEvents=true&orderBy=startTime`,
 {
 headers: {
 Authorization: `Bearer ${accessToken}`,
 },
 },
)

Clerk automatically keeps access tokens current, so you don't need to handle token refresh manually. For a complete working example, check out the BookMate demo application or its source code on GitHub .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions