gmail.users.labels.list()
功能。使用Google's Node.js API时,尝试发送电子邮件时出错。错误是:
{
"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指南。它会读取包含.json
和access_token
的{{1}}文件。 makeBody
function是用来制作编码的正文消息。
在配置变量中我还有:
refresh_token
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'
];
方法。我的设置错了吗? API有变化吗?我错过了什么?
答案 0 :(得分:20)
好的,所以我发现了问题。
问题#1 虽然遵循该教程中的Node.js quickstart guide示例
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
当我得到.json
看起来像:
{
"access_token": "xxx_a_long_secret_string_i_hided_xxx",
"token_type": "Bearer",
"refresh_token": "xxx_a_token_i_hided_xxx",
"expiry_date": 1451721044161
}
生成的代码只考虑 教程代码中的auth/gmail.readonly
范围。
所以我删除了第一个.json
,添加了我的最终范围数组中的范围(我在问题中发布)并再次运行教程设置,接收新标记。
问题#2
在传递给我发送的API的对象中:
{
auth: auth,
userId: 'me',
message: {
raw: raw
}
}
但这是错误的,message
密钥应该被称为resource
。
这是我在教程代码中添加的内容:
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)
});
}
用以下内容打电话:
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);
});
答案 1 :(得分:0)
因此,对于任何试图从其API发送测试电子邮件但无法进行此工作的人,您必须执行以下操作:
步骤1: 替换
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
与此:
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'
];
步骤2: 在Google示例代码的末尾添加以下内容:
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('Receiverofyouremail@mail.com', 'whereyou'resendingstufffrom@gmail.com', 'This is your subject', 'I got this working finally!!!');
const gmail = google.gmail({version: 'v1', auth});
gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: raw
}
}, function(err, response) {
return(err || response)
});
}
fs.readFile('credentials.json', 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);
});
第3步(可选)
删除此行:
authorize(JSON.parse(content), listLabels);
这些:
/**
* Lists the labels in the user's account.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listLabels(auth) {
const gmail = google.gmail({version: 'v1', auth});
gmail.users.labels.list({
userId: 'me',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const labels = res.data.labels;
if (labels.length) {
console.log('Labels:');
labels.forEach((label) => {
console.log(`- ${label.name}`);
});
} else {
console.log('No labels found.');
}
});
}
(因此您不会在控制台中获得随机标签)