无法通过nodejs发送带有googleapis服务帐户和JWTauthentication的邮件

我试图发送一个电子邮件使用服务帐户和JWT身份validation,并不断获取和一个非常无益的消息错误: { code: 500, message: null }

此代码片段来自以下StackOverflow链接: 无法通过nodejs中的google api发送邮件

这似乎是解决scheme是将参数中的密钥更改为resource而不是message但它不适合我。 这很奇怪,因为在文档( https://developers.google.com/gmail/api/v1/reference/users/messages/send )中的JS示例中,它声称密钥仍然是message

我正在用身份validation

 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 )

图书馆的意见似乎是说资源也是正确的属性 我错过了什么?

据github上的@ryanseys说

您无法使用JWT授权Gmail API请求,因此您必须使用OAuth 2.0,因为它需要授权给特定的用户。 否则,你可以做一些非常黑暗的事情,比如发送消息冒充别人。 Google API资源pipe理器使用OAuth 2.0进行身份validation,这就是它的工作原理。 有关更多信息,请参阅https://developers.google.com/gmail/api/auth/about-auth

正如您可以在nodejs , auth: OAuth2Client 通过google api发送邮件失败中看到的,他们使用OAuth2客户端进行身份validation。 目前没有办法让您使用GMail API发送消息,而无需作为特定的GMail用户进行身份validation。 服务帐户无法像普通用户那样访问GMail。

希望这可以帮助别人尝试使用服务帐户发送邮件!