繁体   English   中英

Node.js中发送格子API

[英]Send Grid API in Node.js

我正在尝试获取联系人列表并检查 email 收件人是否打开了 email。 我找到了这段代码并尝试但得到了 401。

import config from "@server/config"
sgClient.setApiKey(config.sendgridKey)
  const headers = {
    "on-behalf-of": "my account user name"  // I put my account user name here
  }

  const request = {
    url: `/v3/subusers`,
    method: "GET"
  } as any
  const list = await sgClient
    .request(request)
    .then()
    .catch((err) => {
      console.log("list", err.response.body)
    })

我需要为 header“代表”填写什么? 子用户的用户名是什么? 有没有什么例子可以让“电子邮件打开”事件发生? 我正在使用 Node.js。

谢谢!

Twilio SendGrid 开发人员在这里。

您尝试使用的API 是子用户 API ,而不是联系人 API。子用户 API 用于管理子用户,您可以向这些帐户申请信用并从中发送电子邮件。 子用户仅适用于 Pro 或 Premier email 帐户或高级营销活动帐户。

但是,即使您使用联系人 API来获取联系人列表,这也不是查看他们是否打开了 email 的方法。

您应该改为注册Event Webhook 使用 Event Webhook,SendGrid 将向您发送有关在 SendGrid 处理您的电子邮件时发生的事件的 Webhook 请求。 这些 事件包括“processed”、“delivered”、“opened”和“clicked”,文档中还有更多

要处理事件 Webhook,您需要自己创建一个可以接收传入的 HTTP 请求的端点。 这是使用 Express 的文档中的示例

const express = require('express');
const app = express();
app.use(express.json());

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
});

app.post('/event', function (req, res) {
  const events = req.body;
  events.forEach(function (event) {
    // Here, you now have each event and can process them how you like
    processEvent(event);
  });
});

var server = app.listen(app.get('port'), function() {
  console.log('Listening on port %d', server.address().port);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM