【问题标题】:SendGrid API Returns "{List} does not exist"SendGrid API 返回“{List} 不存在”
【发布时间】:2017-01-20 14:56:18
【问题描述】:

我正在尝试将电子邮件地址添加到 SendGrid 列表,这是我帐户中唯一存在的列表。我可以成功检索列表并发送电子邮件,但我无法将电子邮件添加到联系人列表。

这是我的方法(按照Email API Reference 上的示例并在Authentication API Reference 上进行身份验证):

<?php
$API_KEY = getenv('SENDGRID_API_KEY'); // Environment variable sourced correctly

$request_url =  "https://api.sendgrid.com/api/newsletter/lists/email/add.json";
$headers = array(
    "Authorization: Bearer $API_KEY",
    "ContentType: application/json"
);
$data = array(
    "email" => "email@example.com",
    "name" => 'Stackoverflow'
);
$params = array(
    'api_user'  => $sendgrid_user,
    'api_key'   => $sendgrid_pass,
    'list'      => "TestList",
    'data'      => json_encode($data)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$resp = curl_exec($ch);
curl_close($ch);

print_r($resp);

下面代码的输出是:

{
    "error": "TestList does not exist"
}

我知道 TestList 存在,因为我也尝试过 List all lists [GET],这向我展示了这一点:

{
    "lists": [
        {
            "id": XXXXXX,
            "name": "TestList",
            "recipient_count": 1
        }
    ]
}

我做错了什么?我错过了什么?

【问题讨论】:

    标签: php curl sendgrid


    【解决方案1】:

    您混淆了 API 的两个不同版本。您正在通过 v3 验证和获取您的列表,但尝试添加到 v2。

    在 v3 中,收件人与列表是一对多的关系,因此您需要 create the recipient,然后是 add it to your list

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2021-06-09
      • 2020-06-12
      相关资源
      最近更新 更多