{
using (HttpClient client = new HttpClient())
{
try
{
// Get a new access token
string accessToken = await GetAccessToken(client);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(/Bearer/, accessToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(/application/json/));
HttpResponseMessage response = await client.PostAsync(bcODataEndpoint, new StringContent(jsonContent, System.Text.Encoding.UTF8, /application/json/));
if (!response.IsSuccessStatusCode)
{
if (response.StatusCode == HttpStatusCode.BadRequest)
{
// Handle Bad Request error
string responseContent = await response.Content.ReadAsStringAsync();
// Log responseContent for debugging
// throw new CustomException(/Bad Request: / + responseContent);
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
// Handle Unauthorized error
// throw a CustomException(/Unauthorized/);
}
else
{
// Handle other HTTP status codes or exceptions
// throw new CustomException(/Other error: / + response.ReasonPhrase);
}
}
}
catch (HttpRequestException ex)
{
// Handle network-related errors
// throw new CustomException(/Network error: / + ex.Message);
}
}
}
{
var values = new Dictionary<string, string>
{
{ /grant_type/, /refresh_token/ },
{ /refresh_token/, refreshToken },
{ /client_id/, clientId },
{ /client_secret/, clientSecret }
};
var responseString = await response.Content.ReadAsStringAsync();
{
throw new Exception(/Failed to refresh access token./);
}
return tokenResponse[/access_token/];
}
{
No = invoiceNumber.ToString().Trim(),
Sell_to_Customer_No = accountNumber.ToString().Trim(),
Company_Bank_Account_Code = bankAccountName.ToString().Trim(),
Shortcut_Dimension_1_Code = branchLabel.ToString().Trim(),
Shortcut_Dimension_2_Code = insurancelabel.ToString().Trim(),
Salesperson_Code67594 = salespersoncode.ToString().Trim(),
Posting_Description = PostingDe.ToString()
};
SyncInvoiceDetails(invoice, service);