Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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节点js-未定义gapi_Javascript_Node.js_Google Api_Gmail_Gmail Api - Fatal编程技术网

Javascript Gmail API节点js-未定义gapi

Javascript Gmail API节点js-未定义gapi,javascript,node.js,google-api,gmail,gmail-api,Javascript,Node.js,Google Api,Gmail,Gmail Api,我正在使用Google Gmail API中的以下函数从用户那里获取所有消息 /** * 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

我正在使用Google Gmail API中的以下函数从用户那里获取所有消息

/**
 * 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 initialRequest=gapi.client.gmail.users.messages.list({ ^ ReferenceError:未定义gapi


我尝试用google替换gapi,因为我已经初始化了var google for api模块,但没有成功。我哪里出了问题,有什么帮助吗?

我也面临同样的问题。。 GAPI仅用于客户端javascript,而不是带有Nodejs的服务器端

1) 从Node.js Quickstart设置示例后

2) 尝试使用此功能列出您帐户中最近的电子邮件并获取该电子邮件:

function getRecentEmail(auth) {
  const gmail = google.gmail({ version: 'v1', auth });
  gmail.users.messages.list({ auth: auth, userId: 'me', maxResults: 1, q: 'subject:()' }, function (err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }

    // Get the message id which we will need to retreive tha actual message next.
    var message_id = response['data']['messages'][0]['id'];

    // Retreive the actual message using the message id
    gmail.users.messages.get({ auth: auth, userId: 'me', 'id': message_id }, function (err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      }
      // console.log(response['data']);

      var to = response['data']['payload']['headers'][4].value;
      var from = response['data']['payload']['headers'][5].value;
      var subject = response['data']['payload']['headers'][6].value;
      var email_body_raw = response['data']['payload']['body']['data'];

      // Convert encoded email body message into human readable text message
      data = email_body_raw;
      buff = new Buffer.from(data, 'base64');
      email_body_readable = buff.toString();

      console.log('content:\n' + email_body_readable + '\n__________\nto: ' + to + '\nfrom: ' + from + '\nsubject: ' + subject + '\n__________\n');
    });
  });
}


fs.readFile('credentials.json', function processClientSecrets(err, content) {
  if (err) {
    console.log('Error loading client secret file: ' + err);
    return;
  }
  //authorize(JSON.parse(content), sendMessage);
  authorize(JSON.parse(content), getRecentEmail);
});


希望这有帮助!

尝试使用
window.gapi
global.gapi
@AnuragSinghBisht这两种方法都不起作用:(@Arihant您应该将库包含到您的code@LEQADA你说的是哪个库?我已经包含了以下内容:var fs=require('fs');var readline=require('readline');var google=require('googleapis');var googleAuth=require('google-auth-library');@Arihant我说的是你代码中的
gapi
。你在哪里初始化它?我觉得它应该是某个地方定义的库。你不能只是开始使用一些从未定义过的变量。