Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Getting Started >

oAuth2 for desktop applications - finally did it

Started by Darren Lawrence -   in Getting Started


I finally figured it out and got working oAuth2 and an API wrapper.

I needed a wrapper to enable me to access Xero from a c# Desktop application.
I have searched the documentation and examples, I have tried to follow the documentation for the new API and constantly found dead ends, bad examples and to top it off, the new API Auth is not easy to understand at all .

The biggest issue I have with this new API is that you have to login to Xero to create the tokens , our system does not have logins for everyone who needs to send invoices to Xero.

For the moment. I will save the token, load it and refresh if needed (dealt with internally) then if it fails an admin will have to login so the re-authentication can be performed. This is a stupid step but I have no choice here.
Hopefully it wont happen that often.

I am willing to share the code but never used Git to store anything before so please bear with me while I build it. The code is a little messy but it works for what I need.

In summary

I use a HttpListener that I found here

I start the http listener on the required port (specified in the Redirect URL)

I build the Url and open it in the default browser. then set a wait loop to wait for 60 seconds (this can be customized)

When you authenticate the Xero login the listener picks up the redirect and extracts the code, I then use HttpClient to request the exchange of the token for the access and refresh tokens.

Using task.Run and task.wait I can convert the Async calls to Sync calls and make the API calls wait for the responses back from Xero.

I am now in the process of creating a simplified wrapper for the API calls using the Net-Standard oAuth2 nuget package

I used to use the oAuth1 API Wrapper which gave a much slicker way to work like linq but this new API is not what I would call easy to use.

If you want a copy of the working project in its current state (its going to take time to tidy up and fully document) I am more than happy to send a link to download it.

I will gradually build all the corresponding wrapper methods to replicate all the Xero API calls and make them more user friendly

For Example. to get 1 invoice you have to call the GetInvoiceAsync which returns a single item in a list

result._Invoices which is a list then you have to get the single item for that invoice, a collection of invoices are result._invoices also is silly. it shoudl return a List<Invoice>
Complete clone of NetStandard with my own Auth process included so you dont need to install the NetStandard nuget package for the models. Plus it will work better for WinForms applications.


Darren Lawrence