2021-08-19 12:27:01 +00:00
|
|
|
import { google } from "googleapis";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-03-26 15:51:19 +00:00
|
|
|
|
2022-04-04 20:26:14 +00:00
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { encodeOAuthState } from "../../_utils/encodeOAuthState";
|
2021-11-03 10:47:52 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const credentials = process.env.GOOGLE_API_CREDENTIALS!;
|
|
|
|
const scopes = [
|
|
|
|
"https://www.googleapis.com/auth/calendar.readonly",
|
|
|
|
"https://www.googleapis.com/auth/calendar.events",
|
|
|
|
];
|
2021-03-26 15:51:19 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-08-19 12:27:01 +00:00
|
|
|
if (req.method === "GET") {
|
|
|
|
// Get token from Google Calendar API
|
2021-10-13 11:35:25 +00:00
|
|
|
const { client_secret, client_id } = JSON.parse(credentials).web;
|
2022-04-04 20:26:14 +00:00
|
|
|
const redirect_uri = WEBAPP_URL + "/api/integrations/googlecalendar/callback";
|
2021-10-12 09:35:44 +00:00
|
|
|
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
|
2021-03-26 15:51:19 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const authUrl = oAuth2Client.generateAuthUrl({
|
|
|
|
access_type: "offline",
|
|
|
|
scope: scopes,
|
|
|
|
// A refresh token is only returned the first time the user
|
|
|
|
// consents to providing access. For illustration purposes,
|
|
|
|
// setting the prompt to 'consent' will force this consent
|
|
|
|
// every time, forcing a refresh_token to be returned.
|
|
|
|
prompt: "consent",
|
2021-11-03 10:47:52 +00:00
|
|
|
state: encodeOAuthState(req),
|
2021-08-19 12:27:01 +00:00
|
|
|
});
|
2021-03-26 15:51:19 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(200).json({ url: authUrl });
|
|
|
|
}
|
2021-04-21 22:10:48 +00:00
|
|
|
}
|