Tuesday, September 3, 2013

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

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 Function
Thats how simple it can be.

10 comments:

  1. Approving the access will involve your client to enter the Username,password and then hitting the 'Authorize' button. Right?

    ReplyDelete
  2. Please give a full example code since I am very new to this.

    ReplyDelete
  3. It would be highly appreciated if You give us an example for public application for windows form.

    ReplyDelete
  4. Nice Post. Thanks for sharing. But I will share a helpful tips. We all want to get Google top ranking with our content/posts. But many time we are unable to get this. Do you know why? Google want Fresh and Unqiue content. But how you can understand my content is free and Unqiue. For this use Plagiarism Detector tools. But many of them don’t use Google. This is the first tool I am going to share which will use Google to get give that your content Unique or not. The big part is that this tools is free. So try this free Plagiarism Software and get first page rank easily.

    ReplyDelete
  5. Free plagiarism checker for avoiding plagiarism, try best anti plagiarism detection tool for detecting plagiarism online.

    ReplyDelete
  6. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  7. What six sigma does is minimize defects in the company's core process, whatever that process may be, thereby smoothing out and speeding up the delivery of product or service. sigma media player is used to eliminate costly mistakes because every mistake affects the bottom line.

    ReplyDelete
  8. keep posting like this it’s really very good idea, you are awesome! avg.com/retail

    ReplyDelete
  9. Those who are searching over internet IT assignment help can contact with us now. We are the best assignment writing service provider in melbourne, Australia. Our Academic assignment writers available 24*7 hours for the students, if you really want to need IT assignment help, java assignment help, programming assignment help, r programming assignment help, Computer science assignment help online at cheapest price and get high distinction grades.

    ReplyDelete