Javascript 使用googleapis';发送邮件失败;nodejs中的服务帐户和JWT身份验证

Javascript 使用googleapis';发送邮件失败;nodejs中的服务帐户和JWT身份验证,javascript,node.js,google-api,gmail,google-apis-explorer,Javascript,Node.js,Google Api,Gmail,Google Apis Explorer,我正在尝试使用服务帐户和JWT身份验证发送电子邮件,并不断收到一条非常没有帮助的消息并出错:{code:500,message:null} 此代码段来自以下StackOverflow链接: 似乎解决方案是将参数中的键改为resource,而不是message,但这对我不起作用。这很奇怪,因为在docs()中的JS示例中,它声称密钥仍然是message 我正在和你进行身份验证 var jwtClient = new google.auth.JWT(config.SERVICE_EMAIL, con

我正在尝试使用服务帐户和JWT身份验证发送电子邮件,并不断收到一条非常没有帮助的消息并出错:
{code:500,message:null}

此代码段来自以下StackOverflow链接:

似乎解决方案是将参数中的键改为
resource
,而不是
message
,但这对我不起作用。这很奇怪,因为在docs()中的JS示例中,它声称密钥仍然是
message

我正在和你进行身份验证

var jwtClient = new google.auth.JWT(config.SERVICE_EMAIL, config.SERVICE_KEY_PATH, null, config.ALLOWED_SCOPES);
然后发送电子邮件与

jwtClient.authorize(function(err, res) {
  if (err) return console.log('err', err);

  var email_lines = [];

  email_lines.push("From: \"Some Name Here\" <rootyadaim@gmail.com>");
  email_lines.push("To: hanochg@gmail.com");
  email_lines.push('Content-type: text/html;charset=iso-8859-1');
  email_lines.push('MIME-Version: 1.0');
  email_lines.push("Subject: New future subject here");
  email_lines.push("");
  email_lines.push("And the body text goes here");
  email_lines.push("<b>And the bold text goes here</b>");

  var email = email_lines.join("\r\n").trim();

  var base64EncodedEmailSafe = new Buffer(email).toString('base64').replace(/\+/g, '-').replace(/\//g, '_');

  var params = {
    auth: jwtClient,
    userId: "myaddress@gmail.com",
    resource: {
      raw: base64EncodedEmailSafe
    }
  };

  gmail.users.messages.send(params, function(err, res) {
    if (err) console.log('error sending mail', err);
    else console.log('great success', res);
  });
}
jwtClient.authorize(函数(err,res){
if(err)返回console.log('err',err);
var电子邮件行=[];
电子邮件行。推送(“发件人:\“此处的某个姓名”);
电子邮件线路。推送至:hanochg@gmail.com");
email_lines.push('Content-type:text/html;charset=iso-8859-1');
email_lines.push('MIME-Version:1.0');
email_lines.push(“主题:此处为新的未来主题”);
电子邮件行。推送(“”);
email_lines.push(“正文文本在这里”);
email_lines.push(“粗体文本在此显示”);
var email=email_lines.join(“\r\n”).trim();
var base64encodemailsafe=new Buffer(email).toString('base64').replace(//\+/g,'-').replace(//\//g,'-');
变量参数={
auth:jwtClient,
用户ID:“myaddress@gmail.com",
资源:{
原始:基本安全
}
};
gmail.users.messages.send(参数,函数(err,res){
if(err)console.log('error sending mail',err');
else console.log('great success',res);
});
}
库中的注释似乎表明资源也是正确的属性()


我错过了什么?

根据github上的@ryanseys

你不能用JWT授权Gmail API请求,你必须使用OAuth 2.0,因为它需要对特定用户进行身份验证。否则你就可以做一些非常不光彩的事情,比如发送模仿他人的消息。Google API Explorer是用OAuth 2.0进行身份验证的,这就是它工作的原因。有关更多信息,请参阅

正如您在,
auth:OAuth2Client
中所看到的,他们正在使用OAuth2客户端进行身份验证。目前,如果您没有作为特定的GMail用户进行身份验证,就无法使用GMail API发送消息。服务帐户无法像普通用户那样访问GMail


希望这能帮助其他人尝试使用服务帐户发送邮件!

那么你是说,即使我拥有该特定域的所有管理员权限,也不可能在没有用户交互(用户直接手动验证)的情况下获取用户电子邮件?