【发布时间】:2020-01-24 22:18:59
【问题描述】:
我很难连接到 Gmail API。我已经完成了设置过程,并在 OAuth 2.0 Playground 上获得了一个工作令牌。我正在尝试从 Node.js / Express / Nodemailer 服务器上的表单发送邮件。当我尝试从表单发送邮件时,我在终端中收到以下错误消息:
[0] [nodemon] starting `node Index.js`
[0] Server listening on port 3001
[0] (node:44987) UnhandledPromiseRejectionWarning: Error: unauthorized_client
[0] at Gaxios.request (/Users/arnepedersen/code/arnelamo/playground-react/my-site/00-email-test/node_modules/gaxios/build/src/gaxios.js:70:23)
[0] at processTicksAndRejections (internal/process/task_queues.js:85:5)
[0] at async OAuth2Client.refreshTokenNoCache (/Users/arnepedersen/code/arnelamo/playground-react/my-site/00-email-test/node_modules/google-auth-library/build/src/auth/oauth2client.js:169:21)
[0] at async OAuth2Client.refreshAccessTokenAsync (/Users/arnepedersen/code/arnelamo/playground-react/my-site/00-email-test/node_modules/google-auth-library/build/src/auth/oauth2client.js:194:19)
[0] at async OAuth2Client.getAccessTokenAsync (/Users/arnepedersen/code/arnelamo/playground-react/my-site/00-email-test/node_modules/google-auth-library/build/src/auth/oauth2client.js:214:23)
[0] (node:44987) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
[0] (node:44987) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我的 index.js 看起来像这样:
const express = require('express')
const bodyParser = require('body-parser')
const nodemailer = require('nodemailer')
const app = express()
// npm install nodemailer googleapis
const { google } = require("googleapis");
const OAuth2 = google.auth.OAuth2;
const oauth2Client = new OAuth2(
// ClientID
"*****",
// Client Secret
"*****",
// Redirect URL
"https://developers.google.com/oauthplayground"
);
oauth2Client.setCredentials({
refresh_token: "*****"
});
const accessToken = oauth2Client.getAccessToken()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.post('/api/form', (req, res) => {
nodemailer.createTestAccount((err, accounteé) => {
const htmlEmail = `
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "arne.pedersen.dev@gmail.com",
clientId: "some_id",
clientSecret: "some_secret",
accessToken: accessToken
}
});
let mailOptions = {
from: 'arne.pedersen.dev@gmail.com',
to: 'arne.pedersen.dev@gmail.com',
replyTo: 'arne.pedersen.dev@gmail.com',
subject: 'New message!',
text: req.body.message,
html: htmlEmail
}
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err)
}
console.log('Message sent: %s', info.message)
console.log('Message URL: %s', nodemailer.getTestMessageUrl(info))
})
})
})
if (process.env.NODE_ENV === "production") {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"))
})
}
const PORT = process.env.PORT || 3001
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`)
})
【问题讨论】:
-
您刚刚发布了您的客户 ID 和密码,您需要立即撤销它们。
-
题外话,我建议不要在 SO 上发布客户机密。
-
糟糕,对此感到抱歉????
-
您使用什么范围以及在哪里定义它们?此外,OAuth 访问令牌的生命周期为 1 小时,我建议您从 the Google Developer Console 的项目管理中获取您的凭据和令牌,您可以按照说明操作 here。
标签: javascript node.js express gmail-api nodemailer