From what I've learned, there are two ways to work with the Google API in Chrome extensions: setting up OAuth and via Google Identity API. I've chosen to work with the ladder.
So I have no problems viewing calendars and events, but now I want to create events. I've been reading calendars using the following:
chrome.identity.getAuthToken({ interactive: true }, async function (token) {
//initialization (think they're all the same)
let init = {
method: "GET",
async: true,
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
contentType: "json",
};
async function getCalendarId() {
return new Promise((resolve, reject) => {
fetch(
"https://www.googleapis.com/calendar/v3/calendars/primary",
init
)
.then((response) => response.json()) // Transform the data into json
.then(function (data) {
console.log(data["id"]);
var id = data["id"];
resolve(id);
});
});
}
calendarId = await getCalendarId();
await fetch(
"https://www.googleapis.com/calendar/v3/calendars/" +
calendarId +
"/events",
init
)
.then((response) => response.json()) // Transform the data into json
.then(async function (data) {
//get today to next week's events
for (var i = 0; i < data["items"].length; i++) {
//console.log(data["items"][i]);
if (data["items"][i]["end"] == undefined) {
console.log("found undefined");
} else {
//found a valid event
}
}
});
});