Search code examples
c#restsalesforceexacttarget

Fire TriggeredSends from ExactTarget's API using HttpClient REST


I've read along the way that Salesforce (I'm extremely new to this 3rd party platform) has a FUEL SDK which one can use instead of the version (using HttpClient -- REST instead of SOAP).

Please correct me if using FUEL SDK is the only way to go about requesting Salesforce's endpoints. Currently I am attempting to hit ExactTargets's API endpoints using HttpClient. These are the tutorials I've been basing my code off of:

Wanted Result:

To be able to request a Triggered Send email based off a template inside of ExactTarget.

Problem:

The Salesforce endpoint continuously returns a 404. I am able to receive the authorization token successfully. The GetAccessToken method is omitted for brevity

https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key:MyExternalKey/send

I do not understand why the 2nd POST request to //www.exacttargetapis.com/..... returns a 404 but the authorization works. This leads me to believe that I do not have to use the FUEL SDK to accomplish triggering a welcome email.


Code:

    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;
    }

I have also tried using Advanced REST Client using the following:

URL: https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key:MyExternalKey/send

POST

Raw Headers:

  • Content-Type: application/json
  • Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Raw Payload:

{
    "From": {
        "Address": "[email protected]",
        "Name": "Code@"
    },
    "To": {
        "Address": "[email protected]",
        "SubscriberKey": "[email protected]",
        "ContactAttributes": {
            "SubscriberAttributes": {
                "Region": "West",
                "City": "Indianapolis",
                "State": "IN"
            }
        }
    },
    "OPTIONS": {
        "RequestType": "ASYNC"
    }
}

Solution

  • Issue was my App in the AppCenter was pointing to the incorrect login for MarketingCloud =(