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";
|
2022-05-02 20:39:35 +00:00
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
2021-11-03 10:47:52 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const scopes = [
|
|
|
|
"https://www.googleapis.com/auth/calendar.readonly",
|
|
|
|
"https://www.googleapis.com/auth/calendar.events",
|
|
|
|
];
|
2021-03-26 15:51:19 +00:00
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
let client_id = "";
|
|
|
|
let client_secret = "";
|
|
|
|
|
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
|
2022-05-02 20:39:35 +00:00
|
|
|
const appKeys = await getAppKeysFromSlug("google-calendar");
|
|
|
|
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
|
|
|
|
if (typeof appKeys.client_secret === "string") client_secret = appKeys.client_secret;
|
|
|
|
if (!client_id) return res.status(400).json({ message: "Google client_id missing." });
|
|
|
|
if (!client_secret) return res.status(400).json({ message: "Google client_secret missing." });
|
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
|
|
|
}
|