I'm building a chrome extension, in which I utilize the google calendar api to programmatically create a calendar by scraping a webpage. The flow is as such.
User clicks Button in pop-up window If already authenticated then it scrapes the page and correctly creates the calender. If not authenticated, then a pop up shows up asking the user to login. After the user logs in and authenticates, a call back function SHOULD be fired which then proceeds with scraping the page.
The call back is not firing, and to get the call back to be called, a subsequent click of the button is required.
Here is my getAuthTokenCall:
chrome.identity.getAuthToken({ interactive: true }, function (token) {
let init = {
method: "POST",
async: true,
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
body: JSON.stringify({
summary: "2021-2022 Academic Year",
}),
contentType: "json",
};
fetch("https://www.googleapis.com/calendar/v3/calendars", init)
.then((response) => {
console.log(response);
if (response.status == 401) {
console.log("the response was 401");
chrome.identity.removeCachedAuthToken(
{ token: token },
function () {
// This forces a rerun of the funciton
createCalender();
}
);
}
return response.json();
})
.then(function (data) {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, data.id);
});
});
});
});
Here is my manifest.json:
{
"name": "DraftToCalender",
"description": "Taking draft my sched and adding classes to Google calender",
"version": "0.1.0",
"manifest_version": 2,
"key": "big key here",
"oauth2": {
"client_id": "google o-auth client key here",
"scopes": [
"https://www.googleapis.com/auth/calendar"
]
},
"content_security_policy": "script-src 'self' https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js; object-src 'self'",
"icons": {
"16": "./obj-16x16.png",
"32": "./obj-32x32.png",
"48": "./obj-48x48.png",
"128": "./obj-128x128.png"
},
"web_accessible_resources": ["script.js", "script1"],
"content_scripts": [
{
"matches": [
"https://draftmyschedule.uwo.ca/secure/builder.cfm"
],
"js": [
"content.js"
]
}
],
"browser_action": {
"default_title": "blahblah",
"default_popup": "./index.html"
},
"permissions": [
"identity",
"storage",
"activeTab"
]
}