简体   繁体   English

创建联系人并将其添加到列表 SendinBlue C#

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

I want to use SendinBlue API on my website but I am facing two problems.我想在我的网站上使用 SendinBlue API,但我面临两个问题。 I want to create a new contact from an email and add this new contact to a contact list on the SendinBlue website.我想通过电子邮件创建一个新联系人,并将此新联系人添加到 SendinBlue 网站上的联系人列表中。 In order to do it, I am following this page https://developers.sendinblue.com/reference/createcontact为了做到这一点,我正在关注此页面https://developers.sendinblue.com/reference/createcontact

First problem: When I am trying to execute the code, an error occured telling me that the key is already in the dictionnary on this line (I obviously replaced "YOUR API KEY" by v3 API Key) :第一个问题:当我尝试执行代码时,发生错误,告诉我密钥已经在该行的字典中(我显然用 v3 API 密钥替换了“您的 API 密钥”):

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

Second problem: After that, when I am addign the new created contact to the API, I have an error on this line telling me that "One or mutliple erros occured" :第二个问题:之后,当我将新创建的联系人添加到 API 时,这一行出现错误,告诉我“发生了一个或多个错误”:

CreateUpdateContactModel result = apiInstance.CreateContact(createContact);

Here is my code:这是我的代码:

// 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();
}

Does anyone already faced one of this problem ?有没有人已经遇到过这个问题之一?

Thank you谢谢

First issue: You are trying to add new value to the dictionary, where the value in dictionary already exists.第一个问题:您正在尝试向字典添加新值,其中字典中的值已存在。 It could be initialized from config file, or input from any previous run?它可以从配置文件初始化,或者从任何以前的运行输入? You should check if the value is present, if missing - you will add it:您应该检查该值是否存在,如果缺失 - 您将添加它:

// 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");

Second issue:第二个问题:

One ore multiple errors occured发生了一个或多个错误

Usually this exception is thrown as Multi-exception , that groupes them together.通常这个异常作为Multi-exception ,将它们组合在一起。 What you need is to check what is inside the InnerException property or get StackTrace .您需要的是检查InnerException属性中的内容或获取StackTrace From that you should be able to see details, what is the problem.从中你应该能够看到细节,问题是什么。


Note for the end, I would recommend following examples on the github since it seems you are using their library for C#.最后请注意,我建议在 github 上遵循以下示例,因为您似乎正在将他们的库用于 C#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM