I am using the following code to retrieve an access_token and I get an error “The underlying connection was closed: An unexpected error occurred on a send”… any idea why?
public static string InfusionsoftUrl = “https://api.infusionsoft.com”;
public class AccessTokenRequest
{
public string client_id { get; set; }
public string client_secret { get; set; }
public string code { get; set; }
public string grant_type { get; set; }
public string redirect_uri { get; set; }
}
public class AccessTokenResponse
{
public string access_token { get; set; }
public string refresh_token { get; set; }
public long expires_in { get; set; }
}
public void CreateOrUpdateAccessToken(string urlLeftPart, string code)
{
var request = new AccessTokenRequest()
{
client_id = “myclientid”,
client_secret = “myclientsecret”,
code = code,
grant_type = “authorization_code”,
redirect_uri = urlLeftPart + “/callback/infusionsoft”,
};
using (var client = new HttpClient())
{
string json = JsonConvert.SerializeObject(request);
var content = new StringContent(json, Encoding.UTF8, “application/json”);
var response = client.PostAsync(InfusionsoftUrl + “/token”, content).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject(jsonResponse);
}