好的,所以我发现了问题.
问题#1 在遵循Node.js快速入门指南时,该教程中的示例有
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); });