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. 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
4) store that Xero "session" in your .net session and redirect user to the page to approve access
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
6) create a repository and you now have access
7) a simple query to populate a drop down list
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. 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"))
Return xSession
End Function
4) store that Xero "session" in your .net session and redirect user to the page to approve access
Session("xeroSession") = xs
Response.Redirect(xs.GetUserAuthorizationUrl)
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
xs.ExchangeRequestTokenForAccessToken(Request("oauth_verifier"))
End If
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"
ddlAccounts.DataBind()
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"
End FunctionThats how simple it can be.