【发布时间】:2020-08-22 19:04:14
【问题描述】:
我正在使用 sendgrid api-keys 发送批量电子邮件,我想测试它是否是有效的 api-key。是否有任何功能可以测试 nodejs 中的 api-key。
【问题讨论】:
我正在使用 sendgrid api-keys 发送批量电子邮件,我想测试它是否是有效的 api-key。是否有任何功能可以测试 nodejs 中的 api-key。
【问题讨论】:
有一个端点可以检索在GET https://api.sendgrid.com/v3/scopes 处为给定键启用的范围。每个有效密钥似乎都在这里工作,无效密钥为 401。此外,它返回范围,以便您可以检查 api 密钥对应用程序是否有效,因此对于发送邮件,它应该包含 mail.send。
从their docs 提取的示例(搜索“范围”):
var http = require("https");
var options = {
"method": "GET",
"hostname": "api.sendgrid.com",
"port": null,
"path": "/v3/scopes",
"headers": {
"on-behalf-of": "The subuser's username. This header generates the API call as if the subuser account was making the call",
"authorization": "Bearer <<YOUR_API_KEY_HERE>>"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write("{}");
req.end();
【讨论】: