google.gmail() is giving an error TypeError: google.gmail is not a function?
google.gmail() is giving an error TypeError: google.gmail is not a function?
我在我的网站上添加了一个 gmail.js 文件来集成 Gmail。但是当我在另一个文件中需要 gmail.js 时,Node.js 会抛出一个类型错误。我的 gmail.js 文件中有以下代码。我在一个文件上分别使用了相同的代码并且它有效。我已经正确安装了所有模块。
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(["test@test.com"]);
});
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;
})
})
}
api 已经更改,因为它处于 alpha 阶段,我刚刚阅读了它。
我必须将 googleapis 导入为
var {google} = require('googleapis');
以下文字引自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');
解释了这两行之间的区别 。
我在我的网站上添加了一个 gmail.js 文件来集成 Gmail。但是当我在另一个文件中需要 gmail.js 时,Node.js 会抛出一个类型错误。我的 gmail.js 文件中有以下代码。我在一个文件上分别使用了相同的代码并且它有效。我已经正确安装了所有模块。
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(["test@test.com"]);
});
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;
})
})
}
api 已经更改,因为它处于 alpha 阶段,我刚刚阅读了它。 我必须将 googleapis 导入为
var {google} = require('googleapis');
以下文字引自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');
解释了这两行之间的区别