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.

18 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
  10. Online Assignment Expert is a milestone-setting assignment help providing company classifying under academics writing of different types. Our trait to make sure that we bestow assignment help without any mistakes. The same has one hearts and minds of over a million students around the globe. Our engineering assignment help experts say that this is what you aim for in your studies. We are no different. We have ensured this motto is followed by each of our engineering assignment writing experts, who are also research scholars. Our experts are certified and experienced professionals in the Solidworks Assignment Help discipline.

    ReplyDelete
  11. Assignment Firm service providing all college students best Assignment Help. Our experts provide 100% plagiarism free content for college and university students. Thank you.

    ReplyDelete
  12. Home Mart is a site about Home Improvement, Furniture, Home Appliances and many more.
    Check out the best
    furniture nz
    furniture sale

    ReplyDelete
  13. We are one of the best custom writing companies as we submit our papers on time and students will have a satisfactory time to go over the essay trying to counter check for any mistake or error which might need urgent correction. Order a custom assignment help and get the best results.

    ReplyDelete
  14. One should know which company has the best writers whose essay is free from grammatical errors. Grammatical errors reduce the quality of a custom paper especially those who want to buy nursing papers online and those seeking for custom assignment services.

    ReplyDelete
  15. Is myassignmenthelp fake? Find out all the reviews of MyAssignmentHelp.

    ReplyDelete
  16. The world is full of businesses but do you know how important they are? You know there are very big ones like Apple, BP and Amazon and very small ones like your local shop or window-cleaner. Grandad explains how all these businesses contribute to our daily life as importantly as the air we breathe and the food we eat. Without businesses he believes we would still be living in caves or mud huts, we would spend all our time searching for food and water. There would be no schools, no doctors, no police, no government,no cars, no television, nearly nothing, This is one of a series of articles aimed by Grandad at grandchildren everywhere. Grandad https://adobe.cheapsoftwaredownload.net/adobe-photoshop.html learned many things over his lifetime in business. These articles can give you a flying start in life.

    ReplyDelete
  17. The agency is best writing firm offering payment for Thesis Proofreading Service professionally by providing top priority for the quality of the Dissertation Proofreading Services which rates us highest among the rest. They offer Nursing Essay Writing Service.

    ReplyDelete