Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
        })
    })
}
like image 635
Abhansh Giri Avatar asked Feb 13 '18 08:02

Abhansh Giri


2 Answers

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');
like image 81
Abhansh Giri Avatar answered Sep 30 '22 04:09

Abhansh Giri


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.

like image 32
neriag Avatar answered Sep 30 '22 05:09

neriag