6

I struggled for about a day trying to authenticate against the Pardot API. It didn't like how I was trying to post the message body. So I wanted to post the solution that worked for me. If you have any tips or alternatives I'd like to hear them.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

var url = "https://pi.pardot.com/api/login/version/3";

//(Edit) Shorter way to pass the parameters below
//var postData = new List<KeyValuePair<string, string>>
//{
//    new KeyValuePair<string, string>("email", "<value>"),
//    new KeyValuePair<string, string>("password", "<value>"),
//    new KeyValuePair<string, string>("user_key", "<value>")
//};

var postData = new Dictionary<string, string>
{
    {"email", "<value>"},
    {"password", "<value>"},
    {"user_key", "<value>"}
};

var httpContent = new FormUrlEncodedContent(postData);

using (var client = new HttpClient())
{
    HttpResponseMessage response = client.PostAsync(url, httpContent).Result;

    if (response.IsSuccessStatusCode)
    {
        string resultValue = response.Content.ReadAsStringAsync().Result;
    }
}

Thanks!

pwDev
  • 945
  • 8
  • 13
  • Unclear about what is the issue. You say your solution worked. What is your question? – Nkosi Jun 01 '16 at 21:34
  • Not so much a question as publishing a solution to a problem/question I had. Since i had struggled and couldn't find a quick fix i wanted to save someone else the trouble. that was all. If you have anything to add to the solution please contribute. – pwDev Jun 01 '16 at 21:59
  • @pwDev SO follows a Q&A format. Could you add an answer, accept it, and edit your question instead? https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ – galdin Jan 21 '19 at 11:29

0 Answers0