Enter the Share Your Success Sweepstakes 2024 For the Chance to Win Great Prizes!

Unable to get refresh token in c#

user453699
Rookie
0 Votes

Hi,

I am integrating Constant Contact API v3 in .net application. I am trying to get the refresh token but it's throwing me 400 error.

Do you have any code example with c# and .net?

 

Here is my code.

 

// Make authorization header with API Key:API Secret and Base64 encode
string credentials = $"{clientId}:{clientSecret}";
string authHeader = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));

//// Create request URL
string requestUri = $"https://authz.constantcontact.com/oauth2/default/v1/token?code={refreshToken}&grant_type=authorization_code&redirect_uri={ConfigurationManager.AppSettings["RedirectURL"]}";



HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest;
System.Diagnostics.Debug.Assert(ServicePointManager.SecurityProtocol == SecurityProtocolType.Tls12);

request.Method = System.Net.Http.HttpMethod.Post.ToString();
request.Accept = "application/json";
request.Headers["x-ctct-request-source"] = "sdk.NET." + GetWrapperAssemblyVersion().ToString();
request.ContentType = "application/x-www-form-urlencoded";

// Add token as HTTP header
request.Headers.Add("Authorization", authHeader);

// Initialize the response
HttpWebResponse response = null;
RawApiResponse rawApiResponse = new RawApiResponse();

// Now try to send the request
try
{
response = request.GetResponse() as HttpWebResponse;
// Expect the unexpected
if (request.HaveResponse == true && response == null)
{
throw new WebException("Response was not returned or is null");
}
foreach (string header in response.Headers.AllKeys)
{
rawApiResponse.Headers.Add(header, response.GetResponseHeader(header));
}

rawApiResponse.StatusCode = response.StatusCode;

if (response.StatusCode != HttpStatusCode.OK &&
response.StatusCode != HttpStatusCode.Created &&
response.StatusCode != HttpStatusCode.Accepted &&
response.StatusCode != HttpStatusCode.NoContent)
{
throw new WebException("Response with status: " + response.StatusCode + " " + response.StatusDescription);
}
}
catch (WebException e)
{
response = e.Response as HttpWebResponse;
rawApiResponse.IsError = true;
}
finally
{
if (response != null)
{
string responseText = null;

// Get the response content
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
responseText = reader.ReadToEnd();
}
response.Close();
if (rawApiResponse.IsError && responseText.Contains("error_message"))
{
rawApiResponse.Info = RawApiRequestError.FromJSON<IList<RawApiRequestError>>(responseText);
}
else
{
rawApiResponse.Body = responseText;
}
}
}

 

 

Request/Response which I got through fiddler.

 

 

1 REPLY 1
John__B
Employee
0 Votes

Hello user453699,

 

Thank you for reaching out to Constant Contact API Developer Support. My team is here to assist outside software developers with questions about building into Constant Contact's API.

 

It looks like we've already followed up with you directly via email, but I wanted to close the loop here as well. If you haven't received our reply, please let us know via email at webservices@constantcontact.com, and reference case number 32443741.

 

Please have a look and let us know if you have any other questions!

 

Regards,


John B.
API Support Specialist
Did I answer your question? If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
Resources
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up