node.js @ sendgrid / mail错误:未经授权

时间:2018-12-19 14:49:07

标签: node.js angular http email sendgrid

我想从我的应用程序(角度)发送电子邮件。 我尝试在node.js的背面使用@ sendgrid / mail,但是它不起作用。

错误是:

(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
    at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
    at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
    at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
    at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

也许有些身体可以帮助我吗?

请在下面找到我的app.js

var sendgrid = require('@sendgrid/mail');
app.post('/sendemail', (request, response) => {
    const body = request.body;
    const email = body.email;
    console.log('sendMail');


    SENDGRID_APY_KEY = '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';
    sendgrid.setApiKey(process.env.SENDGRID_APY_KEY);
    const msg = {
      to: 'ambre.naude@infogene.fr',
      from: 'infogene69@gmail.com',
      subject: 'Votre mot de passe provisoire',
      text: 'Bonjour !',
      html: '<p>Bonjour!</p><p>Voici votre mot de passe provisoire:</p> <p>Cordialement,</p> <p>Infogène Lyon</p>',
    };

    sendgrid.send(msg);
    console.log(msg);

  });

1 个答案:

答案 0 :(得分:0)

问题出在下面

SENDGRID_APY_KEY = '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';
    sendgrid.setApiKey(process.env.SENDGRID_APY_KEY);

启动服务器时,需要将sendgrid api密钥作为env变量传递。上面的代码查找SENDGRID_APY_KEY作为env变量。

要在不传递env变量的情况下使其正常工作,

删除process.env并将SENDGRID_APY_KEY声明为变量。

var SENDGRID_APY_KEY = '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';
    sendgrid.setApiKey(SENDGRID_APY_KEY);

要将SENDGRID_APY_KEY作为环境变量传递:

SENDGRID_APY_KEY='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' node app.js // or however you are starting up the server

如果您确实将密钥作为环境变量传入,则需要从该环境变量中设置sendgrid api,如下所示:sendgrid.setApiKey(process.env.SENDGRID_APY_KEY)

此外,您应该重置/续订您的API密钥(假设您发布的密钥是您当前的密钥)