使用HttpClient REST从exactttarget的API触发triggeredsend
本文关键字:API 触发 triggeredsend exactttarget HttpClient REST 使用 | 更新日期: 2023-09-27 18:11:39
我一路读到Salesforce(我对这个第三方平台非常陌生)有一个可以使用的FUEL SDK
而不是版本(使用HttpClient -- REST
而不是SOAP
)。
如果使用FUEL SDK
是请求Salesforce端点的唯一方法,请纠正我。目前,我正试图使用HttpClient
击中ExactTargets的API端点。这些是我的代码的基础教程:
- https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/messageDefinitionSends.htm
- https://developer.salesforce.com/docs/atlas.en-us.mc-getting-started.meta/mc-getting-started/get-access-token.htm
想要的结果:
能够请求基于ExactTarget
内部模板的Triggered Send
电子邮件。
问题:
Salesforce端点连续返回404。我能够成功地接收授权令牌。为简洁起见,省略了GetAccessToken
方法
https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key MyExternalKey/发送
我不明白为什么第二个POST
请求//www.exacttargetapis.com/.....
返回404,但授权工作。这使我相信我不必使用FUEL SDK
来完成触发欢迎电子邮件。
代码:
private const string requestTokenUrl = "https://auth.exacttargetapis.com/v1/requestToken";
private const string messagingSendUrl = "https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends";
private string exactTargetClientId = ConfigurationManager.AppSettings["ExactTargetClientId"];
private string exactTargetClientSecret = ConfigurationManager.AppSettings["ExactTargetClientSecret"];
private string TriggerEmail(User model, string dbName)
{
var etExternalKeyAppSetting = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.Equals(dbName));
if (etExternalKeyAppSetting != null)
{
string etExternalKey = ConfigurationManager.AppSettings[etExternalKeyAppSetting];
HttpClient client = new HttpClient
{
BaseAddress = new Uri(string.Format(@"{0}/key:{1}/send", messagingSendUrl, etExternalKey)),
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Bearer", this.GetAccessToken())
}
};
try
{
var postData = this.CreateExactTargetPostData(model.Email, etExternalKey);
var response = client.PostAsync(client.BaseAddress
, new StringContent(JsonConvert.SerializeObject(postData).ToString()
, Encoding.UTF8
, "application/json")).Result;
// get triggered email response
if (response.IsSuccessStatusCode)
{
dynamic result = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
}
}
catch (Exception ex)
{
string message = ex.Message;
}
}
return "testing";
}
private object CreateExactTargetPostData(string email, string extKey)
{
var fromData = new
{
Address = ConfigurationManager.AppSettings["AwsSenderEmail"],
Name = "Test"
};
var subscriberAttributes = new { };
var contactAttributes = new
{
SubscriberAttributes = subscriberAttributes
};
var toData = new
{
Address = email,
//SubscriberKey = extKey,
//ContactAttributes = contactAttributes
};
var postData = new
{
From = fromData,
To = toData
};
return postData;
}
我也尝试使用Advanced REST Client
使用以下:
URL: https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key: MyExternalKey/发送
文章
原始标题:
- 内容类型:application/json
- 授权:承载xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
原始载荷:
{
"From": {
"Address": "code@exacttarget.com",
"Name": "Code@"
},
"To": {
"Address": "example@example.com",
"SubscriberKey": "example@example.com",
"ContactAttributes": {
"SubscriberAttributes": {
"Region": "West",
"City": "Indianapolis",
"State": "IN"
}
}
},
"OPTIONS": {
"RequestType": "ASYNC"
}
}
问题是我的应用程序在AppCenter中指向不正确的登录MarketingCloud =(