【发布时间】:2020-09-14 16:14:38
【问题描述】:
我正在尝试使用 nodejs 和 google api auth 和 jwt 发送电子邮件。 我收到以下错误:
code: 400,
errors: [
{
message: 'Precondition check failed.',
domain: 'global',
reason: 'failedPrecondition'
}
这是我的代码:
const jwtClient = new google.auth.JWT(
googleKey.client_email,
null,
googleKey.private_key,
['https://www.googleapis.com/auth/gmail.send'],
'email@xxx-cloud.iam.gserviceaccount.com',
)
function sendMessage(auth, from, to, subject, content) {
// The Gmail API requires url safe Base64
// (replace '+' with '-', and '/' with '_')
var encodedEmail = new Buffer(
'From: ' + from + '\r\n' + 'To: ' + to + '\r\n' + 'Subject: ' + subject + '\r\n\r\n' + content,
)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
var gmail = google.gmail('v1')
var request = gmail.users.messages.send(
{
auth: auth,
userId: 'email@xxx-cloud.iam.gserviceaccount.com',
resource: {
raw: encodedEmail,
},
},
)
}
我尝试了所有方法,但我不断收到此错误。
【问题讨论】:
-
Nodemailer 要容易得多。你试过了吗?
-
我必须用 google api 来做这个......
标签: node.js google-api