C# - Xero API getting the C# code sample working for Private Applications with NuGet


If you don't want to use the NuGet package you can also compile from source (see Xero API compiling the C# code sample from source for Private Applications)

Never having used C# or Visual Studio before I struggled to get the C# code samples working for Xero API, I list my steps here to help others

1) Read through Xero's getting started guide (http://developer.xero.com/documentation/getting-started/getting-started-guide/) but then follow the steps below. 

2) Install openssl (I installed on my windows laptop) and generate a public/private pair of keys (see http://developer.xero.com/documentation/advanced-docs/public-private-keypair/) remember the password during the third "export" command. The file we are interested in is:

public_privatekey.pfx

3) Open Visual Studio (I have 2013) choose File->New->Project then Templates->Visual C#->Store Apps->Windows Desktop->Console Application 

4) Referencing the instructions on https://github.com/XeroAPI/Xero-Net in Visual Studio->Tools->NuGet Package Manager->Package Manager Console at the prompt Bottom of screen) type:
Install-Package Xero.API.SDK

5) in the open file Program.cs add the following to the top

using System.Security.Cryptography.X509Certificates;
using Xero.Api;
using Xero.Api.Core;
using Xero.Api.Example.Applications;
using Xero.Api.Example.Applications.Private;
using Xero.Api.Infrastructure.OAuth;
using Xero.Api.Serialization;

5) inside main() edit as appropriate and add the following:

            X509Certificate2 cert = new X509Certificate2(@"C:OpenSSL-Win32binpublic_privatekey.pfx", "testitout");
            var private_app_api = new XeroCoreApi("https://api.xero.com", new PrivateAuthenticator(cert),
                new Consumer("XE7A2PCGHNEQZYTBKNNT05NAIECSXS", "ILCZGF2ME9CJDQYRZW0ADYI4UWYWBJ"), null,
                new DefaultMapper(), new DefaultMapper());
            var org = private_app_api.Organisation;
            Console.WriteLine("Org Name: " + org.Name);
            var user = new ApiUser { Name = Environment.MachineName };
 


6) Click "Build"->"Build Solution" make sure there are no errors
7) Click  "Debug"->"Start without Debugging"

you should get a popup window's console showing the "Demo Company name"



If you get a crash with the error:

System.Security.Cryptography.CryptographicException was unhandled
  HResult=-2147024810
  Message=The specified network password is not correct.
  Source=mscorlib
 
you have not entered the password correctly in the code - it is case sensitive.

Here's some extra code demonstrating how to fetch and loop through invoices:



or use a  foreach loop:




Make a Comment

Paul

Thank you for this. Got me up and running really quick.

Adin

This page was really helpful. Thanks.

Arif

Thanks a lot. very clear directions. I was able to integrate my app to xero after reading your tips.

Website and Software Development Services
Tweed Heads & Southern Gold Coast

Our network of experienced computer programmers can develop custom software solutions in any language on any platform.

Our Talents

Freelance Power

We maintain a network of local freelance computer programmers that we place in teams to match project requirements. This gives us expertise in a broad range of industries, languages & platforms

Fix, Upgrade or New?

We can fix broken legacy systems, plan and oversee a difficult upgrade, add or unlock new features to your existing software. Or install and set-up new systems from scratch, including hardware and software

Start-ups

We can build solutions fast, Software as a Service (Saas), apps, web applications, desktop software, Minimum Viable Product using Agile Development practices

Websites

We can build simple to complex websites, we can deliver the whole package including site design, branding, logos, domains and hosting

Languages

PHP, MYSQL, C#,
C/C++, Java, JavaScript,
Python, Swift, Meteor,
Objective C

Databases

MYSQL, SQLServer,
MongoDB, NoSQL

Platforms

Linux, Mac, Windows, iPhone, Android, Smart Phones, Tablets

Frameworks

Wordpress, Drupal, Magento, Moodle, Sharepoint Online, Office 365