Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Gmail API-gapi未定义node.js_Javascript_Node.js_Google Api_Gmail Api - Fatal编程技术网

Javascript Gmail API-gapi未定义node.js

Javascript Gmail API-gapi未定义node.js,javascript,node.js,google-api,gmail-api,Javascript,Node.js,Google Api,Gmail Api,我正在使用Google Gmail API中的以下函数来获取来自用户的所有消息,在执行代码时,我得到了gapi not defined error /** * Retrieve Messages in user's mailbox matching query. * * @param {String} userId User's email address. The special value 'me' * can be used to indicat

我正在使用Google Gmail API中的以下函数来获取来自用户的所有消息,在执行代码时,我得到了gapi not defined error

 /**
     * Retrieve Messages in user's mailbox matching query.
     *
     * @param  {String} userId User's email address. The special value 'me'
     * can be used to indicate the authenticated user.
     * @param  {String} query String used to filter the Messages listed.
     * @param  {Function} callback Function to call when the request is complete.
     */
    function listMessages(userId, query, callback) {
      var getPageOfMessages = function(request, result) {
        request.execute(function(resp) {
          result = result.concat(resp.messages);
          var nextPageToken = resp.nextPageToken;
          if (nextPageToken) {
            request = gapi.client.gmail.users.messages.list({
              'userId': userId,
              'pageToken': nextPageToken,
              'q': query
            });
            getPageOfMessages(request, result);
          } else {
            callback(result);
          }
        });
      };
      var initialRequest = gapi.client.gmail.users.messages.list({
        'userId': userId,
        'q': query
      });
      getPageOfMessages(initialRequest, []);
    }
我在代码中定义了以下库

var fs = require('fs'); 
var readline = require('readline'); 
var google = require('googleapis'); 
我得到的错误:

var initialRequest = gapi.client.gmail.users.messages.list({
                       ^
ReferenceError: gapi is not defined

在我错的地方有什么帮助吗?

您需要在运行时使用此脚本,以便加载gapi

<script src="https://apis.google.com/js/api.js"></script>

谷歌为节点上的Gmail API提供了一个很好的快速启动,请参阅:

安装Google API库(如果尚未安装)时,请使用:

您需要通过Oauth2流获得访问API的授权,但是由于您在服务器端(例如节点)工作,因此该流将与许多客户端(例如浏览器)Javascript示例大不相同


有关完整示例,请参阅快速启动中链接的文件:

如果您想使用npm模块,它会自动处理所有问题。
npm install googleapis@27 --save