Nodejs V3 APIλ₯Ό μ¬μ©νμ¬ μ΄λ©μΌμ λ³΄λΌ λ λ€μ μ€λ₯ λ©μμ§κ° νμλ©λλ€.
{
[SendGridError: Response error] message: 'Response error' }
{
statusCode: 401,
body: '{"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]}',
headers: {
server: 'nginx',
date: 'Wed, 31 Aug 2016 18:34:49 GMT',
'content-type': 'application/json',
'content-length': '116',
connection: 'close',
'x-frame-options': 'DENY'
}
}
}
μ΄ λ©μμ§λ λΌμ΄λΈ μλ² νκ²½μμμ κ°μ΄ localhost μΈμ€ν΄μ€μμ μμ±λ©λλ€.
λ€μ μ½λλ μ΄λ©μΌμ 보λ΄λ λ° μ¬μ©λ©λλ€.
var helper = require('sendgrid').mail,
sg = require('sendgrid')(SENDGRID_API_KEY);
var from_email = new helper.Email('[email protected]');
var to_email = new helper.Email(myOwnEmail);
var subject = 'Subject';
var content = new helper.Content('text/plain', 'test');
var mail = new helper.Mail(from_email, subject, to_email, content);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function(err, response) {
console.log(err, response);
if (!err) {
res.send({
message: 'An email has been sent to the provided email with further instructions.'
});
} else {
return res.status(400).send({
message: 'Failure sending email'
});
}
});
μλ νμΈμ @ ninox92 ,
μ΄λ API ν€μ λ¬Έμ κ° μμμ μλ―Έν©λλ€.
λ¨Όμ SENGRID_API_KEYμ κ°μ νμΈνμμμ€.
κ·Έλ° λ€μ https://support.sendgrid.com μμ μ§μ νμ λ¬Έμνμ¬ API ν€μ κΆνμ λ¬Έμ κ° μλμ§ μμλ³Ό μ μμ΅λλ€.
κ°μ¬!
SENGRID_API_KEYκ° νμΈλμμΌλ©° μλ‘ μμ± λ API ν€λ₯Ό μλνμ§λ§ λμΌν μ€λ₯ λ©μμ§κ° μ¬νλμμ΅λλ€.
CURL POST μμ²μ μ¬μ©νμ¬ μ΄λ©μΌμ μ μ‘νμ§λ§ λμΌν API ν€λ₯Ό μ¬μ©νμ¬ μ΄λ©μΌμ μ±κ³΅μ μΌλ‘ μ μ‘ν μμμμ΅λλ€.
λ€μ ν λ² μ§μνμ λ¬Έμνκ² μ΅λλ€.
κ²μ ν μ½λμ SENGRID_API_KEY
μμΌλ©° SENDGRID_API_KEY
λ₯Ό) μλ―Ένλ€κ³ μκ°ν©λλ€.
λ³μ μ΄λ¦ μ€νλ₯Ό μ§μ ν΄ μ£Όμ μ κ°μ¬ν©λλ€. λ΄κ° λ°κΏ κ². κ·ΈλΌμλ λΆκ΅¬νκ³ λ³μλ λ΄ API ν€λ‘ μ¬λ°λ₯΄κ² μ±μμ‘μ΅λλ€.
λ€μ μλνμ§λ§ μ¬ν ν μ μμμ΅λλ€. λ΄κ° μ¬μ©ν μ½λλ λ€μκ³Ό κ°μ΅λλ€.
var helper = require('sendgrid').mail;
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var from_email = new helper.Email('[email protected]');
var to_email = new helper.Email('[email protected]');
var subject = 'Subject';
var content = new helper.Content('text/plain', 'test');
var mail = new helper.Mail(from_email, subject, to_email, content);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON(),
});
sg.API(request, function(err, response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});
μλνλ cURL μμ²λ μ 곡 ν μ μμ΅λκΉ?
λ€μ Hello, World! Sendgrid curl μμ νμ΄μ§μ
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer SENDGRID_API_KEY' \
--header 'Content-Type: application/json' \
--data '{"personalizations": [{"to": [{"email": "<my-own-email>"}]}],"from": {"email": "[email protected]"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'
μλ νμΈμ @ ninox92 ,
λ΄κ° μ 곡 ν μ½λ μνμ μ¬μ©ν΄ λ³΄μ ¨μ΅λκΉ?
μ΄ μ€λ₯λ API ν€κ° μ λλ‘ μ€μ λμ§ μμ μμ μλ―Έν©λλ€.
νκ²½ λ³μλ₯Ό μ¬μ©νμ§ μλ κ²½μ° λ€μμ΄ νμν©λλ€.
var sg = require('sendgrid')('SG.XXXXXXXXXXXXXXXXXXX');
λμΌν μ½λλ‘ μ API KEYλ‘ μ κ³μ μ λ§λ€μμ΅λλ€. μ΄μ μ§μμ΄ μ€λ 걸리기λ₯Ό κΈ°λ€λ¦¬λ λμ μ΄λ©μΌμ λ³΄λΌ μ μμ΅λλ€.
νλ‘μ° ν΄ μ£Όμ μ κ°μ¬ν©λλ€. μ€ν ν΄ μ£Όμ μ κ°μ¬ν©λλ€. λ€λ₯Έ λ¬Έμ κ° λ°μνλ©΄ μλ €μ£Όμμμ€.
κ³ λ§μμ λ§μ΄ λμμ£ΌμΈμ
μλ μλ€ μ λ΄κ° μ¬μ©νλ μ½λλ μ¬κΈ°μ λμΌν μ€λ₯κ° λμκ² λ¬Έμ κ° μμ΅λλ€.
const express = require ( 'express');
const μ± = express ();
var sg = require ( 'sendgrid') (process.env.SENDGRID_API_KEY);
var port = process.env.PORT || 8080;
app.get ( '/', function (req, res) {
var helper = require ( 'sendgrid'). mail;
var fromEmail = new helper.Email ('[email protected] ');
var toEmail = new helper.Email ('[email protected] ');
var subject = 'SendGrid Node.js λΌμ΄λΈλ¬λ¦¬μ Hello World!';
var content = new helper.Content ( 'text / plain', 'Hello, Email!');
var mail = new helper.Mail (fromEmail, subject, toEmail, content);
var request = sg.emptyRequest ({
λ°©λ² : 'POST',
κ²½λ‘ : '/ v3 / mail / send',
λ³Έλ¬Έ : mail.toJSON ()
});
sg.API (μμ², ν¨μ (μ€λ₯, μλ΅) {
if (μ€λ₯) {
console.log ( 'μ€λ₯ μλ΅ μμ λ¨');
}
console.log (response.statusCode);
console.log (response.body);
console.log (response.headers);
});
});
app.listen (port, function () {
console.log ( "λ£κΈ° : *"+ port);
});
λ΄ .env.example νμΌ
SENDGRID_API_KEY = code here / * μ¬κΈ°μ API μ½λκ° μμ΅λλ€ * /
post methodeλ₯Ό μ¬μ©νμ¬ apiλ₯Ό ν΅ν΄ μ΄λ©μΌμ 보λ΄μΌνλ κ²½μ° λ€μκ³Ό κ°μ΄ν΄μΌν©λλ€.
var helper = require ( 'sendgrid'). mail;
router.post ( '/', function (req, res) {
var sg=require('sendgrid')('your api key from your account');
var formEmail= new helper.Email('[email protected]');
var toEmail= new helper.Email(req.body.mail);
var subject='text';
var content = new helper.Content('text/plain', 'Your password has been successfully changed');
var mail = new helper.Mail(formEmail, subject, toEmail, content);
var request =sg.emptyRequest({
method:'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request , function (error , response) {
if(error){
console.log('Error response not valid');
}
});
})
module.exports = λΌμ°ν°;
μ§κΈ μλνλ λ¬Έμ μ λν΄ μ£μ‘ν©λλ€. 202 μ€λ₯λΌκ³ μκ°νμ΅λλ€. μ£μ‘ν©λλ€
λ΄κ° ν λμΌν λ¬Έμ λ₯Ό κ°μ§ μ¬λμ μν΄. SendGridμ ν΄λΌμ΄μΈνΈ λΌμ΄λΈλ¬λ¦¬ μ€ νλλ₯Ό μ¬μ©νλ κ²½μ° μ΅μ μνμΈμ§ νμΈνμμμ€!
λλ C # λΌμ΄λΈλ¬λ¦¬ λ²μ 9.9.0
νκ³ μμκ³ μ΄ μ€λ₯κ° κ°μκΈ° λνλκΈ° μμνμ΅λλ€. 9.10.0
λ²μ μΌλ‘ μ
λ°μ΄νΈνκ³ λ¬Έμ κ° ν΄κ²°λμμ΅λλ€.
ν₯λ―Έλ‘κ²λ μ½λλ₯Ό λ³κ²½ν νμμμ΄ μ λ°μ΄νΈλ‘ ν΄κ²° λ C # Twilio λΌμ΄λΈλ¬λ¦¬μ λκ°μ λ¬Έμ κ° λ°μνμ΅λλ€. Twilioκ° SendGridλ₯Ό μΈμ ν κ²μ λν νμ μ λκΉ?
μ κ²½μ°λ λ€μμ΄μμ΅λλ€.
λ΄ IDE (Intellij CE 2020.10.3)κ° λ΄ sendgrid api ν€ κ° ej : SG.sddDDeq.qwtλ₯Ό κ°μ Έ μ€λ λμ λ΄ enviromet λ³μλ₯Ό μ¬λ°λ₯΄κ² μΈμνμ§ λͺ»νμ΅λλ€. .env νμΌμ κ²½λ‘λ₯Ό μ¬μ©νκ³ μμμ΅λλ€. ej : "C : / Users / Me / MyProjectRootFolder / sendgrid.env "λ₯Ό μ μ‘νκ³ Sendgrid μμ² ν€λμ"Authorization ":"Bearer C : /Users/Me/MyProjectRootFolder/sendgrid.env : "λ‘ μ 곡νλ©΄"The provided authorization λΆμ¬κ° μλͺ»λμμ΅λλ€ ... ".
Soo ... μμ² ν€λλ₯Ό λλ²κ·Ένκ³ μ 곡λ ν€κ° μ ν¨νμ§ νμΈνκ³ (νκ²½ λ³μλ₯Ό μΈμ ν μ μμ) SendGrid λμ 보λμμ λλ©μΈ μΈμ¦ λλ λ¨μΌ λ°μ μ νμΈμ μΆκ°ν΄μΌν©λλ€.
μ΄κ²μ΄ λκ΅°κ°λ₯Ό λκΈ°λ₯Ό λ°λλλ€.
κ°μ₯ μ μ©ν λκΈ
λμΌν μ½λλ‘ μ API KEYλ‘ μ κ³μ μ λ§λ€μμ΅λλ€. μ΄μ μ§μμ΄ μ€λ 걸리기λ₯Ό κΈ°λ€λ¦¬λ λμ μ΄λ©μΌμ λ³΄λΌ μ μμ΅λλ€.