无法通过googleapi服务帐户发送电子邮件

时间:2018-12-12 15:52:02

标签: node.js google-api service-accounts

我正在使用Google API发送电子邮件 我需要通过服务帐号发送 我已经做了有关G西装和证书的工作   但是在发送状态码为400的电子邮件时仍然出现错误

  

代码:400,错误:[{域:“全局”,          原因:“ failedPrecondition”,          消息:“错误请求”}]}

这是代码

const Path = require('path');
const {google} = require('googleapis');
const key = require('../credentials.json');
const base64 = require('base-64')
const oAuthClient = new google.auth.JWT(
    key.client_email,
    null,
    key.private_key,
    ['https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.directory.group'],
    null
);
console.log("auth", oAuthClient)
const gmail = google.gmail("v1", oAuthClient);

const email_lines = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
    "MIME-Version: 1.0\n",
    "Content-Transfer-Encoding: 7bit\n",
    "to: ", "oday.mardini@schongetrunken.de", "\n",
    "from: ", "someone@gmail.com", "\n",
    "subject: ", "the subject", "\n\n",
    "this is the best message"
].join('');
const message = base64.encode(email_lines.trim()).replace(/\+/g, '-').replace(/\//g, '_');

//   const res = await gmail.users.messages.send({'auth':oAuthClient,
//     'userId': 'me',
//     'resource': {
//         'raw': message
//     }})
//     console.log(res)
gmail.users.messages.send({
    auth: oAuthClient,
    userId: 'me',
    resource: {
        raw: message
    }
}, function (err, resp) {
    console.log('err', err);
    console.log('resp', resp);
});

0 个答案:

没有答案