用于在Node.js中发送邮件的Gmail API [英] Gmail API for sending mails in Node.js
问题描述
- 我已遵循Google自己的 Node.js快速入门指南,并成功连接并使用
gmail.users.labels.list()
功能. - 我已经在此处检查了问题/答案,例如这一个(未使用Node.js API,询问),或这一个(类似于
- I have followed Google's own Node.js quickstart guide and successfully connect and use the
gmail.users.labels.list()
functionality. - I have checked for questions/answers here, like this one (that is not using the Node.js API I am asking about), or this one (similar to this one) which apparently is the same problem I have but the solution does not work.
使用 Google的Node.js API 时,出现错误,尝试发送一封电子邮件.错误是:
When using Google's Node.js API I get a error trying to send a email. The error is:
{
"code": 403,
"errors": [{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}]
}
我的设置:
fs.readFile(secretlocation, function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
authorize(JSON.parse(content), sendMessage);
});
function sendMessage(auth) {
var raw = makeBody('myrealmail@gmail.com', 'myrealmail@gmail.com', 'subject', 'message test');
gmail.users.messages.send({
auth: auth,
userId: 'me',
message: {
raw: raw
}
}, function(err, response) {
res.send(err || response)
});
}
函数processClientSecrets
来自我上面提到的Google指南.它读取具有access_token
和refresh_token
的.json
文件. makeBody
函数是用于生成已编码的正文消息的
The function processClientSecrets
is from the Google guide i mentioned above. It reads my .json
file that has my access_token
and refresh_token
. The makeBody
function is a to make a encoded body message.
在配置变量中,我也有:
In the config variabels I have also:
var SCOPES = [
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.send'
];
为什么应该起作用:
- 授权过程适用于
gmail.users.labels.list()
方法. - 如果我在 Google的测试页.
- the authorization process works for the
gmail.users.labels.list()
method. - the message body I'm testing works if I test it at Google's test page.
Why it should work:
我的设置错误吗? API是否进行了更改?我想念什么?
Is my setup wrong? Have there been changes in the API? What am I missing?
推荐答案
好,所以我发现了问题.
Ok, so I found the problem(s).
问题1 遵循 Node.js快速入门指南时,该教程中的示例具有
Problem #1 While following the Node.js quickstart guide the example in that tutorial has
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
当我得到如下所示的.json
时:
And when I got the .json
that looks like:
{
"access_token": "xxx_a_long_secret_string_i_hided_xxx",
"token_type": "Bearer",
"refresh_token": "xxx_a_token_i_hided_xxx",
"expiry_date": 1451721044161
}
生成的那些令牌仅考虑了教程代码中的 范围.
those tokens where produced taking into account only the auth/gmail.readonly
scope in the tutorial code.
所以我删除了第一个.json
,从我的最终作用域数组中添加了作用域(我在问题中发帖了),并再次运行了教程设置,得到了一个新的令牌.
So I deleted the first .json
, added the scopes from my final scope array (i posted in the question) and ran the tutorial setup again, receiving a new token.
问题2
在传递给API的对象中,我正在发送:
In the object passed to the API I was sending:
{
auth: auth,
userId: 'me',
message: {
raw: raw
}
}
但这是错误的,message
键应称为resource
.
but that is wrong, message
key should be called resource
.
这是我添加到教程代码中的内容:
This is what I added to the tutorial's code:
function makeBody(to, from, subject, message) {
var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n",
"to: ", to, "\n",
"from: ", from, "\n",
"subject: ", subject, "\n\n",
message
].join('');
var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail;
}
function sendMessage(auth) {
var raw = makeBody('myrealemail@gmail.com', 'myrealemail@gmail.com', 'test subject', 'test message');
gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: raw
}
}, function(err, response) {
res.send(err || response)
});
}
并使用以下命令进行调用:
And call everything with:
fs.readFile(secretlocation, function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Gmail API.
authorize(JSON.parse(content), sendMessage);
});
这篇关于用于在Node.js中发送邮件的Gmail API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!