使用 HttpClient REST 从 ExactTarget 的 API 触发 TriggeredSends
Fire TriggeredSends from ExactTarget's API using HttpClient REST
我一路读到 Salesforce(我对这个第 3 方平台非常陌生)有一个 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 端点连续 returns 404。我能够成功接收授权令牌。 为简洁起见省略了 GetAccessToken
方法
https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key:MyExternalKey/send
我不明白为什么第二个 POST
请求 //www.exacttargetapis.com/.....
returns 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/send
POST
原始 Headers:
- Content-Type: application/json
- 授权:承载 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
原始负载:
{
"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 的错误登录 =(
我一路读到 Salesforce(我对这个第 3 方平台非常陌生)有一个 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 端点连续 returns 404。我能够成功接收授权令牌。 为简洁起见省略了 GetAccessToken
方法
https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key:MyExternalKey/send
我不明白为什么第二个 POST
请求 //www.exacttargetapis.com/.....
returns 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/send
POST
原始 Headers:
- Content-Type: application/json
- 授权:承载 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
原始负载:
{
"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 的错误登录 =(