-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
95 lines (85 loc) · 2.67 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const { google } = require("googleapis");
require("dotenv").config();
const GOOGLE_PRIVATE_KEY = process.env.private_key.split(String.raw`\n`).join('\n');
const GOOGLE_CLIENT_EMAIL = process.env.client_email;
const GOOGLE_PROJECT_NUMBER = process.env.project_number;
const GOOGLE_CALENDAR_ID = process.env.calendar_id;
const SCOPES = ["https://www.googleapis.com/auth/calendar"];
const jwtClient = new google.auth.JWT(
GOOGLE_CLIENT_EMAIL,
null,
GOOGLE_PRIVATE_KEY,
SCOPES
);
const calendar = google.calendar({
version: "v3",
project: GOOGLE_PROJECT_NUMBER,
auth: jwtClient,
});
const auth = new google.auth.GoogleAuth({
keyFile: "./keys.json",
scopes: SCOPES,
});
const calendarEvent = {
summary: "Test Event added by Node.js",
description: "This event was created by Node.js",
start: {
dateTime: "2023-08-03T09:00:00-02:00",
timeZone: "Asia/Kolkata",
},
end: {
dateTime: "2023-08-04T17:00:00-02:00",
timeZone: "Asia/Kolkata",
},
attendees: [],
reminders: {
useDefault: false,
overrides: [
{ method: "email", minutes: 24 * 60 },
{ method: "popup", minutes: 10 },
],
},
};
const addCalendarEvent = async () => {
auth.getClient().then((auth) => {
calendar.events.insert(
{
auth: auth,
calendarId: GOOGLE_CALENDAR_ID,
resource: calendarEvent,
},
function (error, response) {
if (error) {
console.log("Something went wrong: " + error); // If there is an error, log it to the console
return;
}
console.log("Event created successfully.")
console.log("Event details: ", response.data); // Log the event details
}
);
});
};
addCalendarEvent();
const listCalendarEvents = () => {
calendar.events.list(
{
calendarId: GOOGLE_CALENDAR_ID,
timeMin: new Date().toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: "startTime",
},
(error, result) => {
if (error) {
console.log("Something went wrong: ", error); // If there is an error, log it to the console
} else {
if (result.data.items.length > 0) {
console.log("List of upcoming events: ", result.data.items); // If there are events, print them out
} else {
console.log("No upcoming events found."); // If no events are found
}
}
}
);
};
listCalendarEvents();