【问题标题】:How to Test Sendgrid api key is valid or not Without sending emails?如何在不发送电子邮件的情况下测试 Sendgrid api 密钥是否有效?
【发布时间】:2020-08-22 19:04:14
【问题描述】:

我正在使用 sendgrid api-keys 发送批量电子邮件,我想测试它是否是有效的 api-key。是否有任何功能可以测试 nodejs 中的 api-key。

【问题讨论】:

    标签: node.js sendgrid api-key


    【解决方案1】:

    有一个端点可以检索在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();
    

    【讨论】:

    • 令我震惊的是这不包含在他们的库中......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2020-09-20
    • 2011-01-22
    • 2011-06-06
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多