sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Error 401 Unauthorized when trying to send email from firebase cloud functions

Open slinkstep opened this issue 6 years ago • 1 comments

Issue Summary

I have been trying to send emails from firebase cloud functions but I'm getting a "401 unauthorized error" whenever the funciont executes. I setted the API KEY as a variable in the function (API_KEY)

Steps to Reproduce

  1. This is the first step
  2. This is the second step
  3. Further steps, etc.

Code Snippet


const API_KEY="SG.2cSHW.............."
		sgMail.setApiKey(API_KEY);
		const msg = {
			to: "[email protected]",
			from: '[email protected]',
			subject: '¡Felicidades! Ahora tienes acceso',
			text: 'hola',
			html: '<strong>and easy to do anywhere, even with Node.js</strong>'
    };

	sgMail.send(msg).then(ref => {
    console.log("ref",ref)
    return ref
    
  }).catch(error => {
    console.log("error", error)
    return error
  
  })

Exception/Log

error { Error: Unauthorized
    at Request.http [as _callback] (node_modules/@sendgrid/client/src/classes/client.js:124:25)
    at Request.self.callback (node_modules/request/request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (node_modules/request/request.js:1154:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (node_modules/request/request.js:1076:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
  code: 401,
  message: 'Unauthorized',
  response: 
   { headers: 
      { server: 'nginx',
        date: 'Wed, 11 Mar 2020 04:20:33 GMT',
        'content-type': 'application/json',
        'content-length': '116',
        connection: 'close',
        'access-control-allow-origin': 'https://sendgrid.api-docs.io',
        'access-control-allow-methods': 'POST',
        'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
        'access-control-max-age': '600',
        'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' },
     body: { errors: [Array] } } }


Technical details:

  • sendgrid-nodejs version: ^6.5.4
  • node version: 8

slinkstep avatar Mar 11 '20 04:03 slinkstep

Try logging error.response.body to see the error message(s).

childish-sambino avatar Mar 11 '20 14:03 childish-sambino