简体   繁体   English

不断收到来自 Gmail api、users.messages.get 的无效 id 值错误消息

[英]Keep getting invalid id value error message from Gmail api, users.messages.get

I am trying to read one particular email from my Gmail inbox using Gmail API.我正在尝试使用 Gmail ZDB974238714CA8DE634A7CE1DZ8A 从我的 Gmail 收件箱中读取一个特定的 email。

// Load client secrets from a local file.
fs.readFile('credential/credentials.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  // Authorize a client with credentials, then call the Gmail API.
  authorize(JSON.parse(content), listMessages);
});

function listMessages(oauth2Client, userId, query, callback) {
  const gmail = google.gmail('v1');
  gmail.users.messages.list({
    'auth': oauth2Client,
    'userId': userId,
    'q': query
  }, (err, resp) => {
    if(err) return console.log(err);
    callback(oauth2Client, JSON.stringify(resp.data.messages[0].id));
  });
}

function getMessage(oauth2Client, messageId, callback) {
  const gmail = google.gmail('v1');
  let base64 = require('js-base64').Base64;
  console.log(messageId);
  gmail.users.messages.get({
    'auth': oauth2Client,
    'userId': 'me',
    'id': messageId
  }, (err, resp) => {
    if(err) return console.log(err);
    //console.log(resp);
    //console.log(base64.decode(resp.payload.body.data.replace(/-/g, '+').replace(/_/g, '/')));
  });
}

function authorize(credentials, callback) {
  const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getNewToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client, "me", "Daily Coding Problem", getMessage);
  });
}

I keep getting the following error message after getMessage() is called.调用 getMessage() 后,我不断收到以下错误消息。

errors: [
    {
      domain: 'global',
      reason: 'invalidArgument',
      message: 'Invalid id value'
    }
  ]

I tried using Gmail API directly by filling in the form below and I did get the message I was expecting.我尝试通过填写下面的表格直接使用 Gmail API,我确实收到了我期待的消息。 The Id I used was copied and pasted directly from messageId from getMessage() function.我使用的 Id 是直接从 getMessage() function 中的 messageId 复制和粘贴的。 I do not understand why the same Id string was considered invalid by Gmail messages.get() function.我不明白为什么 Gmail messages.get() function 认为相同的 Id 字符串无效。

在此处输入图像描述

In this line of code:在这行代码中:
callback(oauth2Client, JSON.stringify(resp.data.messages[0].id));

JSON.stringify() is adding extra " marks to the messageId thereby making it invalid. The response from gmail.users.messages.list() returns an array of objects with the messageId as a string so there is no need to stringify it. JSON.stringify()正在向messageId添加额外的"标记,从而使其无效。来自gmail.users.messages.list()的响应返回一个对象数组,其中messageId作为字符串,因此无需对其进行字符串化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解码 users.messages.get() 返回的 gmail 消息 - can't decode gmail message returned by users.messages.get() 从gmail api中的'message id'获取消息 - get message from 'message id' in gmail api 在 gmail Users.messages 中接收未来的内部日期:获取 API - Receiving future internal date in gmail Users.messages: get API 在用户的GMail收件箱中获取邮件 - Get messages in users' GMail inbox Gmail API - 使用 users.threads.list 与 users.messages.list 获得不同的结果 - Gmail API - Getting different results with users.threads.list vs users.messages.list 如何使用 Javascript 从 Chrome 扩展中获取每条 Gmail 邮件的 ID - How to get ID of each gmail message with Javascript from chrome extension (DiscordJS) 尝试在消息 ID 之前获取消息但继续获取最新的消息 - (DiscordJS) Trying to fetch messages before message ID but keep getting the most recent one 使用 gmail api users.messages 在线程中回复:发送不起作用 - Replying in a thread using gmail api users.messages: send is not working GMail API Users.threads.list缺少“消息”字段 - GMail API Users.threads.list Missing “Messages” Field Gmail.js 错误:api.dom.email 使用无效元素/ID 调用 - Gmail.js error: api.dom.email called with invalid element/id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM