将 Gmail 与 OAuth 和 Nodemailer 结合使用的最终方法是什么? [英] What is the definitive way to use Gmail with OAuth and Nodemailer?
问题描述
期望的行为
使用 Gmail
、OAuth2
和 Nodemailer
从服务器端 node.js
文件发送电子邮件.
我的尝试
相关文档
03)
点击Create Project
04)
点击Create
05)
输入项目名称
并点击Create
06)
选择Gmail API
07)
点击Enable
08)
点击Create Credentials
09)
输入所需的设置
10)
为 OAuth 客户端命名并确保将 https://developers.google.com/oauthplayground
添加为 重定向 URI
以便稍后生成 refresh
和 access
令牌
11)
定义同意屏幕设置
12)
点击I'll do this later
和Done
13)
点击Edit
图标,查看您的Client ID
和Client Secret
14)
要生成 access
和 refresh
令牌,请转到
16)
在左栏中,选择 Gmail API v1
并点击 Authorise APIs
17)
如果您登录了多个帐户,请在提示时选择相关帐户
18)
点击允许
19)
点击兑换令牌的授权码
我不确定为什么对 access
令牌进行倒计时,但希望屏幕底部的消息意味着令牌不会过期.
Desired Behaviour
Use Gmail
, OAuth2
and Nodemailer
to send an email from a server side node.js
file.
What I've Tried
Relevant Documentation
https://nodemailer.com/smtp/oauth2
https://nodemailer.com/usage/using-gmail
https://developers.google.com/gmail/api/auth/web-server
Relevant Questions
send emails from MY gmail account with OAuth2 and nodemailer
How do I authorise an app (web or installed) without user intervention?
https://stackoverflow.com/a/47936349
https://stackoverflow.com/a/22572776
There were gaps in the instructions of the above sources and some information was outdated, so the answer below was my final implementation which appears to be working.
I'm posting this solution for confirmation it is best practice and, if it is, to save others time.
The following worked for me, there are two parts:
01) app.js
02) Google
and OAuth2
setup
app.js
var nodemailer = require("nodemailer");
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: local_settings.my_gmail_username,
clientId: local_settings.my_oauth_client_id,
clientSecret: local_settings.my_oauth_client_secret,
refreshToken: local_settings.my_oauth_refresh_token,
accessToken: local_settings.my_oauth_access_token
}
});
var mail = {
from: "John Smith <me@mydomain.com>",
to: "user@userdomain.com",
subject: "Registration successful",
text: "You successfully registered an account at www.mydomain.com",
html: "<p>You successfully registered an account at www.mydomain.com</p>"
}
transporter.sendMail(mail, function(err, info) {
if (err) {
console.log(err);
} else {
// see https://nodemailer.com/usage
console.log("info.messageId: " + info.messageId);
console.log("info.envelope: " + info.envelope);
console.log("info.accepted: " + info.accepted);
console.log("info.rejected: " + info.rejected);
console.log("info.pending: " + info.pending);
console.log("info.response: " + info.response);
}
transporter.close();
});
Google and OAuth Setup
The code above requires the following setup:
01)
Go to https://console.developers.google.com
02)
If you don't have a project, you will be prompted to create one
03)
Click on Create Project
04)
Click on Create
05)
Enter a Project Name
and click Create
06)
Select the Gmail API
07)
Click on Enable
08)
Click on Create Credentials
09)
Enter the required settings
10)
Give the OAuth client a name and ensure you add https://developers.google.com/oauthplayground
as a redirect URI
in order to generate the refresh
and access
tokens later
11)
Define the consent screen settings
12)
Click I'll do this later
and Done
13)
Click on the Edit
icon, to view your Client ID
and Client Secret
14)
To generate access
and refresh
tokens, go to https://developers.google.com/oauthplayground
15)
Click on the cog
icon in the top right, check Use your own OAuth credentials
and enter Client ID
and Client Secret
16)
In the left column, select Gmail API v1
and click Authorise APIs
17)
If you are signed into multiple accounts, when prompted select the relevant account
18)
Click Allow
19)
Click Exchange authorisation code for tokens
I'm not sure why there is a count down on the access
token, but hopefully the message at the bottom of the screen means that the token won't expire.
这篇关于将 Gmail 与 OAuth 和 Nodemailer 结合使用的最终方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!