Note: A useful full video on how to use VB.net in Private Application mode with the Xero API is here.
This is a mirror of useful content that was hosted on http://blog.itqld.com.au/2013/09/simple-access-to-xero-api-example-using.html
However the above site seems to be down and I retrieved a copy from Google Cache. I’m posting a copy here in case google’s cache expires and the source website doesn’t come back online.
Simple Access to the Xero API Example using VB.NET
Recently a client approached me and asked if we could link an application we recently developed with Xero. Of course i said, this should be a breeze, i expect they will have all kinds of useful documentation and samples.I have now spent the better part of a whole day just getting authenticated and pulling a simple list of accounts. in the end, this is done by some simple setup and a few lines of code and nowhere is it documented how easy it really is.
This is an ASP.NET web application sitting in a Dot Net Nuke portal.
Here it is in VB.NET
1) download the xeroaip.dll file with nuget.exe here http://nuget.codeplex.com/releases/view/58939 you can use nuget.exe in the command line to just get the DLL file – check out this blog – http://blog.davidebbo.com/2011/01/installing-nuget-packages-directly-from.html **I’ve mirrored this below too*** the package name is “xeroapi.net“
2) add the DLL to your project in visual studio and make a reference to it in the references node. you are now ready to go.
3) first create a new “session” object
dim xs = newSession()
Public Function newSession() As XeroApi.OAuth.XeroApiPublicSession
Dim xSession = New XeroApi.OAuth.XeroApiPublicSession(“[Application Name]”, CONSUMER_KEY, CONSUMER_SECRET, New DevDefined.OAuth.Storage.Basic.InMemoryTokenRepository)
Dim rt As RequestToken = xSession.GetRequestToken(New System.Uri(“http://call.back.url/application.aspx”))
4) store that Xero “session” in your .net session and redirect user to the page to approve access
Session(“xeroSession”) = xs
5) the user will be returned with a querystring which includes an “oauth_verifier”. pull your original xero “session” from the .net session and have it create the access token
Dim xs = CType(Session(“xeroSession”), XeroApi.OAuth.XeroApiPublicSession)
If Not xs.HasValidAccessToken Then
6) create a repository and you now have access
Dim xr As New XeroApi.Repository(xs)
lblXeroConnected.Text = “Connected to ” & xr.Organisation.Name
7) a simple query to populate a drop down list
ddlAccounts.DataSource = getAccounts(xr)
ddlAccounts.DataTextField = “name”
ddlAccounts.DataValueField = “accountid”
Public Function getAccounts(ByVal xr As XeroApi.Repository) As XeroApi.Linq.ApiQuery(Of XeroApi.Model.Account)
Return From account In xr.Accounts Where account.Status.ToLower = “active” And account.Type.ToLower = “bank”
Thats how simple it can be.
Most of the coverage around NuGet revolves against its clean integration to Visual Studio, which makes adding references to packages as easy as adding references to local assemblies. While this is indeed a key scenario, it is important to note that the core of NuGet is completely decoupled from Visual Studio, and was designed with that goal from day 1.
If we look back at the early days of NuGet, it was in many ways inspired by the ‘Nu’ project (which members have since joined NuGet). What Nu had was a solid command line driven experience to bring down .NET bits to your machine. In their case, it was based on Ruby Gems, but that is an implementation details. Take a look at Rob Reynolds’s original screencast to see what the Nu experience was about.
While we have been planning all along to provide the same experience with NuGet (in addition to the VS experience of course), it’s something that had somewhat fallen off the radar, and it just had not been done. This was unfortunate, because we already had all the plumbing to make it happen, and all it needed was about 10 lines of code to expose this!
So I’m happy to say that we have now filled this little hole by implementing a new ‘install’ command in our NuGet.exe command line tool. Using it couldn’t be any easier, and I’ll walk you through an example.
Where do I get NuGet.exe?
You first need to get NuGet.exe. This is the same tool that package authors have been using to create packages and upload them to the http://nuget.org gallery.
The easiest way to get it is to download it from CodePlex.
You can also obtain it via NuGet itself by installing the package name NuGet.CommandLine (using Visual Studio).
How do I run it?
The best way to demonstrate it is to just show a sample session.
D:\>md \Test D:\>cd \Test D:\Test>nuget list nhi FluentNHibernate 22.214.171.1244 FluentNHibernate 126.96.36.1994 NHibernate 188.8.131.5200 NHibernate 184.108.40.2061 NHibernate 220.127.116.1101 NHibernate 18.104.22.16800 NHibernate.Linq 1.0 NHWebConsole 0.2 SolrNet.NHibernate 0.3.0 D:\Test>nuget install NHibernate 'Iesi.Collections (≥ 1.0.1)' not installed. Attempting to retrieve dependency from source... Done. 'Antlr (≥ 22.214.171.124154)' not installed. Attempting to retrieve dependency from source... Done. 'Castle.Core (≥ 2.5.1)' not installed. Attempting to retrieve dependency from source... Done. Successfully installed 'Iesi.Collections 1.0.1'. Successfully installed 'Antlr 126.96.36.199154'. Successfully installed 'Castle.Core 2.5.2'. Successfully installed 'NHibernate 188.8.131.5200'. D:\Test>tree Folder PATH listing Volume serial number is 26FF-2C8A D:. ├───Antlr.184.108.40.206154 │ └───lib ├───Castle.Core.2.5.2 │ └───lib │ ├───NET35 │ ├───NET40ClientProfile │ ├───SL3 │ └───SL4 ├───Iesi.Collections.1.0.1 │ └───lib └───NHibernate.220.127.116.1100 └───lib D:\Test>dir Antlr.18.104.22.168154\lib Volume in drive D has no label. Volume Serial Number is 26FF-2C8A Directory of D:\Test\Antlr.22.214.171.124154\lib 01/20/2011 05:06 PM 117,760 Antlr3.Runtime.dll
Why would you want to use this instead of the Visual Studio integration?
For most users, the Visual Studio integration will be the right choices. But suppose you want to work much more ‘manually’, and not deal with VS or even with a .csproj file. e.g. all you want is to bring down nhibernate.dll so you can write some code against it, and compile it manually using ‘csc /r:nhibernate.dll MyCode.cs’.
In this scenario, you just want NuGet to download the assemblies for you, and leave the rest to you. It still saves you a lot of time by letting you easily download the bits and all their dependencies, but it doesn’t force you into a development model that may not be want you want.
So I don’t think it’s a feature that the majority of users will use, but it is important to have it for those who need it.