I am trying to attach a file to the Xero invoice and using Xero.NetStandard.OAuth2.Api and the net framework starter app for it.
Below is the code we are using to attach, however it throws an error : A validation exception occurred:The file couldn't be uploaded because it isn't a supported file type.
We are uploading and .png type file also tried using a .pdf file
var xeroToken = TokenUtilities.GetStoredToken(); var utcTimeNow = DateTime.UtcNow;
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
XeroConfiguration XeroConfig = new XeroConfiguration
{
ClientId = ConfigurationManager.AppSettings["XeroClientId"],
ClientSecret = ConfigurationManager.AppSettings["XeroClientSecret"],
CallbackUri = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),
Scope = ConfigurationManager.AppSettings["XeroScope"],
State = ConfigurationManager.AppSettings["XeroState"]
};
if (utcTimeNow > xeroToken.ExpiresAtUtc)
{
var client = new XeroClient(XeroConfig, httpClientFactory);
xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);
TokenUtilities.StoreToken(xeroToken);
}
string accessToken = xeroToken.AccessToken;
string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();
var AccountingApi = new AccountingApi();
var sevenDaysAgo = DateTime.Now.AddDays(-7).ToString("yyyy, MM, dd");
var invoicesFilter = "Date >= DateTime(" + sevenDaysAgo + ")";
var response = await AccountingApi.GetInvoicesAsync(accessToken, xeroTenantId, null, invoicesFilter);
var invoices = response._Invoices;
foreach (Invoice inv in invoices)
{
if (inv.HasAttachments == false && inv.InvoiceID != null)
{
string filePath = ConfigurationManager.AppSettings["AttachmentPath"].ToString();
filePath = Path.Combine(filePath, inv.InvoiceNumber);
string[] filePaths = Directory.GetFiles(filePath);
foreach (var file in filePaths)
{
byte[] contentFileBytes = System.IO.File.ReadAllBytes(file);
Guid invoiceId = Guid.Parse(inv.InvoiceID.ToString());
var attachResponse = await AccountingApi.CreateInvoiceAttachmentByFileNameAsync(accessToken, xeroTenantId, invoiceId, System.IO.Path.GetFileNameWithoutExtension(file), true, contentFileBytes);
}
}
}
Could anyone please help, if there is any sample code for attaching a file to invoice created.