我需要使用 API 向联系人添加标签。
我正在根据 API V3 文档 ( https://developers.activecampaign.com/reference#create-contact-tag ) 使用 Active Campaign 资源,发出 POST 请求。我还有其他请求,例如添加和获取联系人以及以相同方式添加和获取帐户。
但是,发出请求时我只收到 400 Bad request status,包括错误 No tag id provided。我已经检查了标签 ID 和联系人 ID 是否存在。
我正在使用此功能填充联系人标签,我检查并在发送时都填写了。
private void UpsertTag(GetAllTagsOutput output, string contactId, string tagvalue)
{
TagMeta tag = output.Tags.FirstOrDefault(c => c.Tag == tagvalue);
ContactTag contactTag = new ContactTag
{
Contact = contactId,
Tag = tag.Id
};
this.service.Upsert(contactTag);
}
contacttag 类如下所示:
[Api("contactTags")]
public class ContactTag : ActiveCampaignRef
{
/// <summary>
/// Contact ID
/// </summary>
public string Contact { get; set; }
/// <summary>
/// Tag ID
/// </summary>
public string Tag { get; set; }
}
然后我使用
public T Upsert<T>(T item, IDictionary<string, string> parameters = null) where T : ActiveCampaignRef
{
if (IsNestedObject(item))
{
return requestService.Post<T>(GetPath(item), item);
}
var responseValues = requestService.Post<Dictionary<string, object>>(GetPath(item), item, parameters);
return JsonConvert.DeserializeObject<T>(responseValues[GetItemName(item)].ToString());
}
获取路径确实返回预期路径https://youraccountname.api-us1.com/api/3/contactTags
终于有帖子了
public TResponse Post<TResponse>(string path, object data, IDictionary<string, string> parameters = null)
{
return this.Send<TResponse>(path, "POST", data, parameters);
}
private TResponse Send<TResponse>(string path, string method, object data = null, IDictionary<string, string> parameters = null)
{
return JsonConvert.DeserializeObject<TResponse>(this.SendRequest(path, method, data, parameters));
}
private string SendRequest(string path, string method, object data = null, IDictionary<string, string> parameters = null)
{
var finalPath = path;
if (parameters != null && parameters.Any())
{
// Add parameters to the request
var queryString = string.Empty;
foreach (var param in parameters)
{
queryString = queryString + "&" + param.Key + "=" + param.Value;
}
finalPath += "?" + queryString;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + finalPath);
request.Method = method;
request.Headers.Add("API-Token", apiKey);
AttachDataIfNecessary(request, data);
try
{
var webResponse = (HttpWebResponse)request.GetResponse();
using (Stream dataStream = webResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
string response = reader.ReadToEnd();
return response;
}
}
catch (WebException e)
{
throw GetActiveCampaignException(e);
}
}
方法:POST 状态:BadRequest 响应:{"errors":[{"title":"查询参数 contactTag.tag 的值无效","detail":"未提供标签 ID","source":{"parameter": "contactTag.tag"}}]}'