Sendgrid-nodejs: 제곡된 κΆŒν•œ λΆ€μ—¬κ°€ μœ νš¨ν•˜μ§€ μ•Šκ±°λ‚˜ λ§Œλ£Œλ˜μ—ˆκ±°λ‚˜ μ·¨μ†Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€ (401).

에 λ§Œλ“  2016λ…„ 09μ›” 01일  Β·  15μ½”λ©˜νŠΈ  Β·  좜처: sendgrid/sendgrid-nodejs

문제 μš”μ•½

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'
        });
    }
});

기술적 μ„ΈλΆ€ 사항:

  • localhost : Windows 10 x64
  • 라이브 μ„œλ²„ : Heroku
  • sendgrid-nodejs 버전 : 4.0.2
  • npm 버전 : 3.7.5
  • λ…Έλ“œ 버전 : 4.4.5
help wanted question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ™μΌν•œ μ½”λ“œλ‘œ μƒˆ API KEY둜 μƒˆ 계정을 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. 이제 지원이 였래 걸리기λ₯Ό κΈ°λ‹€λ¦¬λŠ” λ™μ•ˆ 이메일을 보낼 수 μžˆμŠ΅λ‹ˆλ‹€.

λͺ¨λ“  15 λŒ“κΈ€

μ•ˆλ…•ν•˜μ„Έμš” @ 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 λŒ€μ‹œ λ³΄λ“œμ—μ„œ 도메인 인증 λ˜λŠ” 단일 λ°œμ‹ μž 확인을 μΆ”κ°€ν•΄μ•Όν•©λ‹ˆλ‹€.

이것이 λˆ„κ΅°κ°€λ₯Ό 돕기λ₯Ό λ°”λžλ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰