【问题标题】:google.gmail() is giving an error TypeError: google.gmail is not a function?google.gmail() 给出错误 TypeError: google.gmail is not a function?
【发布时间】:2018-07-23 13:04:27
【问题描述】:

我包含一个 gmail.js 文件来在我的网站上进行 Gmail 集成。但是当我在另一个文件中需要 gmail.js 时,Node.js 会抛出一个 typeError。我的 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;
        })
    })
}

【问题讨论】:

    标签: node.js express google-api gmail google-api-nodejs-client


    【解决方案1】:

    该 api 已更改,因为它处于 alpha 状态,我刚刚阅读了它。 我必须将 googleapis 导入为

    var {google} = require('googleapis');
    

    【讨论】:

    • 很好,这对我有用,谢谢。但我很好奇这些行之间有什么区别? {} 做了什么?
    • 谢谢!我也有同样的问题。 {} 是什么?
    • @jrpereirajr {} 是 javascript 的符号。在此处阅读有关它的更多信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
    【解决方案2】:

    以下文字引用自Release 26 note:

    重大变化:此库现已针对 es6 模块进行了优化。在以前 您可以像这样导入库的版本:

    const google = require('googleapis');

    在此版本和未来版本中,您必须使用命名导入:

    const {google} = require('googleapis');

    这两行之间的区别解释here。

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 2021-03-07
      • 2022-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2016-06-24
      相关资源
      最近更新 更多