Reputation: 191
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;
})
})
}
Upvotes: 5
Views: 2814
Reputation: 151
The following text is a quote from Release 26 note:
BREAKING CHANGE: This library is now optimized for es6 modules. In previous versions you would import the library like this:
const google = require('googleapis');
In this and future versions, you must use a named import:
const {google} = require('googleapis');
The difference between those two lines is explained here.
Upvotes: 8
Reputation: 191
The api has been changed since it is in alpha and I read it just now. i have to import googleapis as
var {google} = require('googleapis');
Upvotes: 11