簡體   English   中英

創建聯系人並將其添加到列表 SendinBlue C#

[英]Create contact and add it to a list SendinBlue C#

我想在我的網站上使用 SendinBlue API,但我面臨兩個問題。 我想通過電子郵件創建一個新聯系人,並將此新聯系人添加到 SendinBlue 網站上的聯系人列表中。 為了做到這一點,我正在關注此頁面https://developers.sendinblue.com/reference/createcontact

第一個問題:當我嘗試執行代碼時,發生錯誤,告訴我密鑰已經在該行的字典中(我顯然用 v3 API 密鑰替換了“您的 API 密鑰”):

Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

第二個問題:之后,當我將新創建的聯系人添加到 API 時,這一行出現錯誤,告訴我“發生了一個或多個錯誤”:

CreateUpdateContactModel result = apiInstance.CreateContact(createContact);

這是我的代碼:

// I replaced "YOUR API KEY" with my private api key
sib_api_v3_sdk.Client.Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");
var apiInstance = new ContactsApi();
JObject attributes = new JObject();
List<long?> listIds = new List<long?>();
listIds.Add(2);
bool emailBlacklisted = false;
bool smsBlacklisted = false;
bool updateEnable = true;
List<string> smtpBlacklistSender = new List<string>();
try
{
    var createContact = new CreateContact(email, attributes, emailBlacklisted,
                            smsBlacklisted, listIds, updateEnable, smtpBlacklistSender);
    CreateUpdateContactModel result = apiInstance.CreateContact(createContact);
    Debug.WriteLine(result.ToJson());
    Console.WriteLine(result.ToJson());
    Console.ReadLine();
}
catch (Exception exc)
{
    Debug.WriteLine(exc.Message);
    Console.WriteLine(exc.Message);
    Console.ReadLine();
}

有沒有人已經遇到過這個問題之一?

謝謝

第一個問題:您正在嘗試向字典添加新值,其中字典中的值已存在。 它可以從配置文件初始化,或者從任何以前的運行輸入? 您應該檢查該值是否存在,如果缺失 - 您將添加它:

// Instead of this
Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

// You should do something like this
// Note that there might be different method to be called, you have to check what is available
if(!Configuration.Default.ApiKey.HasKey("api-key"))
    Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

第二個問題:

發生了一個或多個錯誤

通常這個異常作為Multi-exception ,將它們組合在一起。 您需要的是檢查InnerException屬性中的內容或獲取StackTrace 從中你應該能夠看到細節,問題是什么。


最后請注意,我建議在 github 上遵循以下示例,因為您似乎正在將他們的庫用於 C#。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM