Categories
express gmail google-api google-api-nodejs-client node.js

google.gmail() is giving an error TypeError: google.gmail is not a function?

I am including a gmail.js file to do the Gmail integration on my website. But when I require gmail.js in another file, Node.js throws a typeError. I have the following code In my gmail.js file. I have used same code separately on a file and it works. I have installed all the modules properly.

    const express = require('express');
const google = require('googleapis');
var gmail = google.gmail('v1');
const googleAuth = require('google-auth-library');
const fs = require('fs');
var async = require('async');
var db = require('./db');
const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
var oauth2Client = null;
fs.readFile('./client_secret.json', function(err, content){
if(err){
console.log("error loading client secret file" + err);
return;
}
var credentials = JSON.parse(content);
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
//temp variables
//i will take token from database
oauth2Client.credentials = {
access_token: access_token,
refresh_token: refresh_token,
token_type: token_type,
expiry_date: expiry_date
}
getThreadIds(["[email protected]"]);
});
function setOAuth2Credentials(token){
oauth2Client.credentials = token;
return true;
}
function getAuthUrl(){
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log(authUrl);
return authUrl;
}
function getToken(code){
oauth2Client.getToken(code, function(err, token){
if(err){
console.log('Error while trying to get access to token ' + err);
return false;
}
oauth2Client.credentials = token;
db.storeGoogleAuthToken(token, function(err, result){
if(err){
console.log("error storing token to database " + err);
return false;
}
return true;
})
})
}