Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Getting Started >

Initial Authentication

Started by Jim Simmonds -   in Getting Started


I feel like I am the only person that cannot get past the first hurdle of getting the access code using the following;

var public_app_api = new XeroCoreApi("https://api.xero.com", new PublicAuthenticator("https://api.xero.com", "https://api.xero.com", "oob",
new MemoryTokenStore()),
new Consumer("your-consumer-key", "your-consumer-secret"), user,
new DefaultMapper(), new DefaultMapper());

var public_contacts = public_app_api.Contacts.Find().ToList();

I have set up my app details with consumer key and secret etc but as soon as i reach public_app_api.Contacts.Find().ToList() the system crashes.

I have checked out the value of the tokenStore and it only ever gets to 'Find' an existing token rather that trying to Add one.

Is there a stage before this that creates a token first? Every snippet of code I have looked at suggests my code above is the first step and that I should be redirected to Xero to get an authentication code.

Any help on this would be much appreciated.

Hey Jim,

When you say it crashes, are you getting any specific type of exception? What detail can you give us about the crash?

Have you been able to use a tool like Fiddler to inspect any requests/responses being issued to/from our API? If so what response code and messages do you see for any failed requests?


Matthew Mortimer (Xero Staff)  

Hi Matt,

Thank you for your response. Basically what is happening is that there is a null response. Within visual studio I can inspect this and the Organisation is empty as follows;

Organisation = 'PublicApi.Organisation' threw an exception of type 'Xero.Api.Infrastructure.OAuth.UnexpectedOauthResponseException'

I initially thought this might have had something to do with the MemoryTokenStore as this in itself is quite confusing with so many different implementations of the same thing within all the examples I have seem. Due to this, in anticipation of us subsequently becoming a Partner at some point in the future I wrote an SQL store for the tokens which works fine.

To clarify where I am with this. I have created an account in the xero developer section to obtain the Key and Secret. I have created a single page webform on an asp.net platform. On this form is a single button that runs the code as shown. So far I have spent about 3 and a half days on these few lines of code!

The only thing that differs is that the code has been converted to VB as can be seem below;

Dim ConsumerKey As String = "xxxxxxxxxx"
Dim ConsumerSecret As String = "xxxxxxxxxx"
Dim UserAgent As String = Environment.MachineName

Dim Consumer As IConsumer = New Consumer(ConsumerKey, ConsumerSecret)
Dim TheUser = New ApiUser() With {.Name = UserAgent}

Dim MemoryTokenStore As ITokenStore = New MemoryAccessTokenStore()
Dim BaseUrl As String = "https://api.xero.com"

Dim MyAuthenticator = New PublicAuthenticator(BaseUrl, BaseUrl, "oob", MemoryTokenStore )
Dim PublicApi = New XeroCoreApi(BaseUrl, MyAuthenticator, Consumer, TheUser, New DefaultMapper(), New DefaultMapper())

' Running the following command fails
Dim Public_Contacts = PublicApi.Contacts.Find().ToList()

I have also tried uploading this to our server to run on the web rather than in my visual studio debugger and the outcome is exactly the same.

Kind regards
Jim Simmonds

Jim Simmonds  

Stack trace... Anyone got any ideas?

UnexpectedOauthResponseException: An unexpected HTTP response was returned while performing OAuth operations

Xero.Api.Infrastructure.OAuth.OAuthTokens.GetToken(String baseUri, IToken consumer, String endPoint, String header) +816

Xero.Api.Infrastructure.OAuth.OAuthTokens.GetRequestToken(IConsumer consumer, String header) +80
Xero.Api.Example.Applications.TokenStoreAuthenticator.GetRequestToken(IConsumer consumer) +233
Xero.Api.Example.Applications.TokenStoreAuthenticator.GetToken(IConsumer consumer) +18
Xero.Api.Example.Applications.TokenStoreAuthenticator.GetToken(IConsumer consumer, IUser user) +74
Xero.Api.Example.Applications.TokenStoreAuthenticator.GetSignature(IConsumer consumer, IUser user, Uri uri, String verb, IConsumer consumer1) +21
Xero.Api.Infrastructure.Http.HttpClient.CreateRequest(String endPoint, String method, String accept, String query) +321
Xero.Api.Infrastructure.Http.HttpClient.Get(String endpoint, String query) +42
Xero.Api.Infrastructure.Http.XeroHttpClient.Get(String endPoint) +177
Xero.Api.Common.XeroReadEndpoint`3.Get(String endpoint, String child) +249
Xero.Api.Common.XeroReadEndpoint`3.Find() +10

Jim Simmonds  

If you catch the exception, can you get any more information out of it?
Alternatively using a tool like fiddler you can capture the HTTP response you're receiving which should provide some insight to your issue.

If I was to guess what's going on, I'd guess you're receiving a precondition failure response (HTTP 418) for not complying with TLS restrictions that have somewhat recently been put in place. The API requires that you use TLS1.1+ to communicate with it. We suggest TLS1.2. You can find more information about doing this with the Xero-Net SDK in this issue on Github

Matthew Mortimer (Xero Staff)  

Hi Matt,

I am looking at this as we speak, you are correct, this does have everything to do with TLS 1.0. I have successfully got my visual studio / local box to work by updating the registry keys. The problem I am currently having is getting my Windows server on 2012 R2 to do the same. I have updated registry keys using IIsCrypto but for some reason it's still fighting me despite several re-boots. There will be a logical solution but until I find it I will have to keep pushing on.

Thanks for your help by the way, I dont think that would have occurred to me for quite some time about the TLS setting.


Jim Simmonds  

Nice one, it's good to see that we've found a root cause.
For what it's worth, I'll add an issue to the git repo to try get TLS errors surfaced a bit better for future users.


Matthew Mortimer (Xero Staff)  


How would I use Fiddler to get the response from the API?. and where would I use this.?

I have downloaded Fiddler to my local machine and used our website to create the connection but it seems nowhere can I see a response from Xero within Fiddler.

Are you able to give me a head start with this, it seems I cannot get the responses out of the API once the code has been uploaded to my server.

The problem right now is that within Visual studio I can get the API to work but as soon as I upload to our server It will not work. I have enabled TLS 1.2 etc... also disabled TLS1.0. but I just cannot get the response message from your API to see what is going on.

Jim Simmonds

Jim Simmonds  

When you drill right down to it, the line that fails within the API dll is;

var authorizeUrl = GetAuthorizeUrl(token, _scope, _redirectOnError);

THIS LINE HERE >>>> Process.Start(authorizeUrl);

It works on my local machine but when the full app is uploaded and run on the production server it crashes with the following;

System.ComponentModel.Win32Exception (0x80004005): Class not registered at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start()


Jim Simmonds  


No thanks to the utterly abysmal instructions and scattered help i finally got it working through sheer brute force trial and error.


Jim Simmonds