[英]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.