我一直在尝试使用 API 检索 SendGrid 事务模板列表。我使用正确的 API 密钥并获得一个空数组,而我的 SendGrid 帐户中存在大约 5 个事务模板。这是回应:
{
"templates": []
}
任何猜测可能是什么问题?
我一直在尝试使用 API 检索 SendGrid 事务模板列表。我使用正确的 API 密钥并获得一个空数组,而我的 SendGrid 帐户中存在大约 5 个事务模板。这是回应:
{
"templates": []
}
任何猜测可能是什么问题?
任何猜测可能是什么问题?
是的,他们的文档可能是!
当我打开 devtools 并看到他们如何从 UI 请求自己的 API 时,我也坚持了这个问题并最终设法解决了这个问题。长话短说 - 必须传递额外的generations=dynamic
查询参数。这是我使用的 C# 代码:
var client = new SendGridClient("key");
var response = await client.RequestAsync(
SendGridClient.Method.GET,
urlPath: "/templates",
queryParams: "{\"generations\": \"dynamic\"}");
使用 API 7.3.0 PHP
require("../../sendgrid-php.php");
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
#Comma-delimited list specifying which generations of templates to return. Options are legacy, dynamic or legacy,dynamic
$query_params = json_decode('{"generations": "legacy,dynamic"}');
try {
#$response = $sg->client->templates()->get();
$response = $sg->client->templates()->get(null, $query_params);
echo $response->body();
exit;
} catch (Exception $e) {
echo '{"error":"Caught exception: '. $e->getMessage().'"}';
}