在Node.js JavaScript中向用户发送消息的gmail API失败 [英] gmail API for sending users messages in nodejs javascript failes
问题描述
我的nodejs程序无法使用Gmail api发送邮件.
My nodejs program fails to send messages using the Gmail api.
来自 Gmail API的解决方案,用于在Node.js中发送邮件对我不起作用.
我使用
var {google} = require('googleapis');
// to and from = "some name <blaw.blaw.com"
function makeBody(to, from, subject, message) {
var str = ["Content-Type: text/plain; charset=\"UTF-8\"\r\n",
"MIME-Version: 1.0\r\n",
"Content-Transfer-Encoding: 7bit\r\n",
"to: ", to, "\r\n",
"from: ", from, "\r\n",
"subject: ", subject, "\r\n\r\n",
message
].join('');
encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail;
}
然后转到Google API资源管理器 https://developers.google.com/apis-explorer/#p/ 输入gmail.users.messages.send以及从上面的make_body生成的字符串.
Then go to the Google API explorer https://developers.google.com/apis-explorer/#p/ enter gmail.users.messages.send and the string generated from the above make_body.
电子邮件将成功发送.所以我知道上面的编码是 好吧.
An email will be successfully sent. So I know the above encoding is ok.
当我的程序尝试使用以下命令发送时,它失败并显示错误
When my program tried to send using the following, it fails with error
错误:原始" RFC822有效负载消息字符串或通过以下方式上载消息 /upload/*需要网址
Error: 'raw' RFC822 payload message string or uploading message via /upload/* URL required
function sendMessage(auth) {
var gmail = google.gmail('v1');
var raw = makeBody('john g <asdfasdf@hotmail.com>', 'john g<asfasdgf@gmail.com>', 'test subject', 'test message #2');
gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: raw
}
}, function(err, response) {
console.log(err || response)
});
}
身份验证令牌很好,因为我可以调用gmail.users.labels.list,并且在使用API资源管理器时使用相同的授权.
The auth token is good since I can call gmail.users.labels.list and I use the same authorization when using the API explorer.
问题1:有谁知道为什么上述方法不起作用?
Q1: Does anyone know why the above does not work?
第二季度:用于发送的Gmail API Node.js中的邮件没有解释为什么将原始电子邮件包装在资源字段中.我只是尝试原始的,但并没有帮助.
Q2: Gmail API for sending mails in Node.js does not explain why the raw email message is wrapped inside a resource field. I tried simply raw and it did not help.
这失败.
gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: raw
}
}, function(err, response) {
console.log(err || response)
});
gmail.users.messages.send({
auth: auth,
userId: 'me',
raw: raw
}, function(err, response) {
console.log(err || response)
});
gmail.users.messages.send({
auth: auth,
userId: 'me',
data: raw
}, function(err, response) {
console.log(err || response)
});
有人知道它在哪里记录了如何通过api资源管理器所要求的请求的主体"吗?
Does anyone know where its documented how to pass the "requested body" the api explorer is asking for?
第三季度:为什么Google API需要在base64编码中进行替换?
Q3: Why does the google api need substitutions in the base64 encoding?
我尝试使用
const Base64 = require("js-base64").Base64
var encodedMail = Base64.encode(str);
当我将其输入到API资源管理器中时,我得到了错误
When I feed this into the API explorer, I get the error
"message":"ByteString的值无效:
"message": "Invalid value for ByteString:
推荐答案
Ohai!对于在这里绊倒的其他人,有几件事.首先-我们现在有一个完整的端到端发送邮件示例:
Ohai! For others that stumble here, a few things. First - we have a complete end to end sample of sending mail now here:
https://github .com/google/google-api-nodejs-client/blob/master/samples/gmail/send.js
第二,上面的答案基本上是正确的:)而不是安装最新版本的google-auth-library
...只需将其从package.json中全部删除即可.入门指南非常非常错误(此问题已得到修复). googelapis
引入了它自己的兼容版本google-auth-library
,因此您真的不想通过安装自己的版本来弄混它:)
Second, the answer above is mostly right :) Instead of installing the latest version of google-auth-library
... just remove it from your package.json all together. The getting started guide was very, very wrong (it has since been fixed). googelapis
brings in it's own compatible version of google-auth-library
, so you really don't want to mess with that by installing your own version :)
这篇关于在Node.js JavaScript中向用户发送消息的gmail API失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!