我正在尝试使用服务帐户和 JWT 身份验证发送电子邮件,并不断收到一条非常无益的消息并出错:{ code: 500, message: null }
此代码片段来自以下 StackOverflow 链接:Failed sent mail through google api in nodejs
似乎解决方案是将参数中的键更改为resource
而不是,message
但它对我不起作用。这很奇怪,因为在文档(https://developers.google.com/gmail/api/v1/reference/users/messages/send)中的 JS 示例中,它声称密钥仍然是message
我正在验证
var jwtClient = new google.auth.JWT(config.SERVICE_EMAIL, config.SERVICE_KEY_PATH, null, config.ALLOWED_SCOPES);
然后发送一封电子邮件
jwtClient.authorize(function(err, res) {
if (err) return console.log('err', err);
var email_lines = [];
email_lines.push("From: \"Some Name Here\" <rootyadaim@gmail.com>");
email_lines.push("To: hanochg@gmail.com");
email_lines.push('Content-type: text/html;charset=iso-8859-1');
email_lines.push('MIME-Version: 1.0');
email_lines.push("Subject: New future subject here");
email_lines.push("");
email_lines.push("And the body text goes here");
email_lines.push("<b>And the bold text goes here</b>");
var email = email_lines.join("\r\n").trim();
var base64EncodedEmailSafe = new Buffer(email).toString('base64').replace(/\+/g, '-').replace(/\//g, '_');
var params = {
auth: jwtClient,
userId: "myaddress@gmail.com",
resource: {
raw: base64EncodedEmailSafe
}
};
gmail.users.messages.send(params, function(err, res) {
if (err) console.log('error sending mail', err);
else console.log('great success', res);
});
}
库中的评论似乎说资源也是正确的属性(https://github.com/google/google-api-nodejs-client/blob/master/apis/gmail/v1.js)
我错过了什么?