Unable to get refresh token in c#
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