I'm very new to the concept of API and nodejs. I have a task to create a simple agent that accesses users events on google calendar to remind him on his upcoming event, but I have been struggling with this error "Error: No access, refresh token or API key is set". I have followed a guide on how to authorize and retrieve the access token but it seems to not work. Here is my main.js file:
const google = require('googleapis').google;
const dfff = require('dialogflow-fulfillment')
const app = express();
const googleConfig = {
clientId: 'xxxxxxxxxxxx',
clientSecret: 'xxxxxxxxxxxx',
redirect: 'https://d5242f9e0cb4.ngrok.io'
};
function createConnection() {
return new google.auth.OAuth2(
googleConfig.clientId,
googleConfig.clientSecret,
googleConfig.redirect
);
}
const defaultScope = [
'https://www.googleapis.com/auth/calendar.readonly'
];
function getConnectionUrl(auth) {
return auth.generateAuthUrl({
access_type: 'offline',
prompt: 'consent', // access type and approval prompt will force a new refresh token to be made each time signs in
scope: defaultScope
});
}
function urlGoogle() {
const auth = createConnection(); // this is from previous step
const url = getConnectionUrl(auth);
return url;
}
// Setting up EJS Views
app.set('view engine', 'ejs');
app.set('views', __dirname);
app.get('/', function (req, res) {
const url = urlGoogle()
return res.render("index", { loginLink: url });
});
app.get('/auth_callback', function (req, res) {
// Create an OAuth2 client object from the credentials in our config file
const OAuth = createConnection()
//if (req.query.error) {
// The user did not give us permission.
//return res.redirect('/error');
//} else {
const {tokens} = OAuth.getToken(code)
OAuth.setCredentials({
access_token: tokens.access_token,
refresh_token: tokens.refresh_token,
expiry_date: (new Date()).getTime() + (1000 * 60 * 60 * 24 * 7)
});
return res.redirect('/');
//});
//}
});
app.post('/', express.json(),(req,res)=>{
const calendar = google.calendar({version: 'v3' , auth:createConnection()});
const agent = new dfff.WebhookClient({
request : req,
response : res
})
function welcome(agent){
agent.add("Hi, I'm Rem. Please click on remind me button if you want to be reminded on your upcoming events!")
}
function listEvents(agent){
return calendar.events.list({
'calendarId': 'primary',
'auth':createConnection(),
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 1,
'singleEvents': true,
'orderBy': 'startTime'
}).then((err,response)=> {
let events = response.result.items;
if (events.length !== 0) {
var event = events[i];
var when = event.start.dateTime;
if (!when) {
when = event.start.date;
}
agent.add('Be ready for '+ event.summary + ' event '+ 'at ' + when )
}
else {
agent.add('You dont have any upcoming events.');
}
});
}
let intenMap = new Map()
intenMap.set('Default_Welcome_Intent',welcome)
intenMap.set('Remind_me',listEvents)
agent.handleRequest(intenMap)
});
// Listen on the port defined in the config file
app.listen("3002", function () {
console.log(`Listening on port 3002`);
});
What could I be missing??