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.

31 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
    Replies

    1. Great Article. Thank you for sharing! Really an awesome post for every one.

      IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      Delete
  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
  18. Whether the Descriptive Essay Paper focuses on a single subject or compares two approaches, our Descriptive Essay Writing Service comes in with Unique Descriptive Essay Writing Format to complete the homework.

    ReplyDelete
  19. Well, we all know that nursing is not an easy task. There are a number of nursing students who get a number of nursing assignments. Being a medical student, it is quite difficult to write on theoretical topics. According to the research, I found that when they are unable to write their nursing assignments by own they search for the nursing assignment help. To deal with the number of assignment tasks they seek help from the online assignment writing services. But as there are many assignment panels in the market they do not get the best nursing assignment, expert.

    ReplyDelete
  20. Those students who are searching for Science Assignment help services in USA can contact with our experts. We are best services providers over the country. Our experts are available round the clock, students can contact them at any time.

    ReplyDelete
  21. Today, two-thirds of college students leave school with at least some debt from college loans. The average debt is approaching $25,000, a figure that includes not just the original amounts borrowed but, for most students, accumulated interest as well. modafinil

    ReplyDelete
  22. You have observed very interesting points ! ps decent internet site . cinema 4d r17 download

    ReplyDelete
  23. Law case studies are one of the tried and tested methods of making students aware of the laws and how the court proceedings are regarding that specific law. Law case study help students to understand the concepts and how these laws can be used to make your case stronger.

    ReplyDelete
  24. wished to thanks for your|positively have the gift. |the most effective|extra on this topic|Howdy|thanks!|however I don’t know if I see all of them center putlocker

    ReplyDelete
  25. Thanks for your post. I've found much useful information I need. I'm a writer and I have to write an article on the similar topic. Don't worry. I won't copy your work. I always use advanced plagiarism checker at https://plagiarismsearch.com to be sure I'm original.

    ReplyDelete
  26. We always confirm to deliver the work before the deadline no matter , how tight the deadline is? Any of our assignments aren't delivered without proofreading and send them to the scholars after multiple revisions so as to supply the simplest quality work.auditing assignment

    ReplyDelete
  27. Online education writing services companies have come up with Online Education Assignment Help for education and teaching studies students in order for them to score straight A’s in their custom education research paper services.

    ReplyDelete