I am trying to construct a Gmail replica but I’ve hit a roadblock in terms of making requests to Gmail API-
here’s my code:
index.js
const express = require("express");
const cors = require("cors");
const passport = require("passport");
const cookieSession = require("cookie-session");
require("./passport-setup");
const CLIENT_URL = "http://localhost:3000/inbox"
const passportSetup = require("./passport-setup");
const GoogleStrategy = require("./passport-setup").OAuth2Strategy;
var { gmail } = require('@googleapis/gmail');
const path = require('path');
const app = express();
app.use(cors({credentials: true, origin: 'http://localhost:3000'}));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(
cookieSession({
name: "session",
keys: ["key1", "key2"],
})
);
app.use(passport.initialize());
app.use(passport.session());
app.get("/", (req, res) => res.send("Hello World"));
app.get("/failed", (req, res) => res.send("Failed!"));
// GET /auth/google
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Google authentication will involve
// redirecting the user to google.com. After authorization, Google
// will redirect the user back to this application at /auth/google/callback
app.get("/google", passport.authenticate('google', {
scope: ['profile',
'email',
'https://mail.google.com/'
],
accessType: 'offline',
prompt: 'consent'
}))
// GET /auth/google/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get("/google/callback", passport.authenticate("google", {
successRedirect: CLIENT_URL,
failureRedirect: "/failed"
}))
app.get(
"/google/callback",
passport.authenticate("google", { failureRedirect: "/failed" }),
);
app.get("/success", (req, res)=>{
if(req.user) {
res.status(200).json({
success: true,
message: "Login success!",
user: req.user,
cookies: req.cookies
});
};
});
app.get('/inbox', (req, res)=> {
gmail.users.messages.list({
userId: 'me',
auth: oauth2Client,
id: message.id,
format: "full",
pageToken: nextPageToken,
}, function(err, response) {
res.send(response);
});
});
app.get("/logout", (req,res)=>{
req.logout();
res.redirect(CLIENT_URL);
})
app.listen(5000, () => console.log(`Server running at port ${5000}!`));
passport-setup.js
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
passport.use(
new GoogleStrategy(
{
clientID: "insert client-id",
clientSecret: "insert client secret",
callbackURL: "http://localhost:5000/google/callback",
},
function (accessToken, refreshToken, profile, done) {
return done(null, profile);
}
)
);
As seen in the following code I’m trying to pull out gmail.users.messages.list but have only been responded to with: “TypeError: Cannot read properties of undefined (reading ‘messages’)”.
me trying to request gmail.users.messages.list:
app.get('/inbox', (req, res)=> {
gmail.users.messages.list({
userId: 'me',
auth: oauth2Client,
id: message.id,
format: "full",
pageToken: nextPageToken,
}, function(err, response) {
res.send(response);
});
});