I am not entirely sure what does CTCT thinks. If any other developers out there need the code here it is for MVC C# [Route("check_session_api")]
public object CheckSessionAPI()
{
if (Session["ResponseAPI"] == null)
{
return "No result";
}
else
{
return Session["ResponseAPI"].ToString();
}
}
[Route("api_request")]
public ActionResult ApiRequest()
{
var url = "https://api.cc.email/v3/idfed" + "?client_id=" + apiKey +
"&redirect_uri=" + redirectURL +
"&scope=contact_data+campaign_data" + "&response_type=code";
return RedirectPermanent(url);
}
[Route("api_response")]
public object ApiResponse()
{
try
{
string code = Request.QueryString["code"];
if (code != null)
{
using (var webClient = new WebClient())
{
byte[] bytes = Encoding.UTF8.GetBytes(apiKey + ":" + client_secret);
var base64 = Convert.ToBase64String(bytes);
var client = new RestClient(tokenURL);
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic " + base64);
request.AddParameter("code", code);
request.AddParameter("redirect_uri", redirectURL);
request.AddParameter("grant_type", "authorization_code");
IRestResponse res = client.Execute(request);
if (res.StatusCode.ToString().ToLower().IndexOf("bad") < 0)
{
Session["ResponseAPI"] = res.Content;
}
}
return RedirectToAction("Index", "Home");
}
return null;
}
catch (Exception ex)
{
return ex;
}
}
[Route("api_refresh")]
public object ApiRefresh(string refresh_token)
{
try
{
using (var webClient = new WebClient())
{
var client = new RestClient(tokenURL);
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(apiKey + ":" + client_secret)));
request.AddParameter("refresh_token", refresh_token);
request.AddParameter("grant_type", "refresh_token");
IRestResponse res = client.Execute(request);
if (res.StatusCode.ToString().ToLower().IndexOf("bad") < 0)
{
///success
Session["ResponseAPI"] = res.Content;
} else
{
//failure
refresh_token = "{hard code the refresh token}";
return ApiRefresh(refresh_token);
}
return Session["ResponseAPI"];
}
}
catch (Exception ex)
{
return ex;
}
} Now the major PROBLEM with this design are: 1) You must hard code the refresh token and good luck with that !!!! 2) You must be logged in into https://developer.constantcontact.com for this whole thing to even work!! That's right. So if you are a developer like me and write this code for a website to collect subscriber emails you are screwed (no other way to put that). Constant Contact Team, You need to fix this and asap otherwise I will be happy to take it away from you to mailchimp. UGH!!!
... View more