1

I'm implementing an internal procedure to generate invoices from my database. In using .NET Core. I installed the Xero.Api.SDK.Core v. 1.1.4

I find the Xero documentation quite difficult to understand and a bit up to date. I took a look to the Github repository and they refer only to OAuth 2.0. In my case I don't want any user interaction because I want a background process.

I created an account as developer and a demo company for test.

enter image description here

I googled a bit and I found in a site an example how to connect to the api (I couldn't find any details in the Xero documentation).

X509Certificate2 cert = new X509Certificate2(@"public_privatekey.pfx", "password");

var api = new Xero.Api.Example.Applications.Private.PrivateAuthenticator(cert);

var private_app_api = new XeroCoreApi("https://api.xero.com", 
                                      new PrivateAuthenticator(cert),
                                      new Consumer("ClientId", "ClientSecret"), 
                                      null, null);

Now, I thought, I should use the api. I tried to read the list of contacts:

var contacts = private_app_api.Contacts;
var list = contacts.FindAsync().Result;

The result is the following error:

System.AggregateException: 'One or more errors occurred. (oauth_problem=consumer_key_unknown&oauth_problem_advice=Consumer%20key%20was%20not%20recognised)'

Inner Exception UnauthorizedException: oauth_problem=consumer_key_unknown&oauth_problem_advice=Consumer%20key%20was%20not%20recognised

Then, I downloaded the repository from Github. I copy and paste the ClientId and ClientSecret and run the tests. No one test is passed!

enter image description here

My goal is to create a contact or find one in the list, create an invoice and save the invoice in PDF format in my storage.

How can I do that? Is there any example for that?

PS: In the developer forum the login doesn't work and the Postman collection is old and it doesn't either.

Update

I was desperate. I tried to connect to the apis in another way with XOauth. Worst. I created the connection as Xero explains on Github.

enter image description here

When I try to connect with

xoauth connect

a new tab in my browser is opened and...

enter image description here

Postman Update

I followed the steps with Postman. I opened the collection in my Postman. In the environment I updated client_id and client_secret from the Xero Developer site and then the following configurations:

re_directURI: https://developer.xero.com

scopes: offline_access profile email accounting.transactions

Auth URL: https://login.xero.com/identity/connect/authorize

Access Token: https://identity.xero.com/connect/token

I opened Get started api, Generate token and I gave the same result.

enter image description here

Enrico
  • 3,592
  • 6
  • 45
  • 102
  • Look on the youtube channel. There's a video showing how to do a machine-to-machine connection which you'll need for a background process. I'm doing a Windows service, which is also background. https://www.youtube.com/channel/UC7DA_vntKKChsenzpL7QWPg/videos – droopsnoot May 10 '20 at 17:23
  • Your code is using the old-style OAuth 1 calls, and while you can still create apps that way using a special link, you'll only be able to do that until June. So there's not a lot of point in creating new stuff using that now. – droopsnoot May 10 '20 at 17:24
  • This is in the documentation and in the Github repository. I found everything very confused and messy. I tried to look the Youtube channel, it is worse: most of the video are old. There is only one video https://www.youtube.com/watch?v=Zcf_64yreVI quite chaotic and I can't understand how to do that. – Enrico May 10 '20 at 17:44
  • Oh, that's the one I was thinking of, that cleared up a few things for me. I don't use the API now, I use XOAuth to get the token in the first place, then a HTTP POST to refresh it when needed, and HTTP get or post as required to interact with Xero. My problem with the sample code is I'm using VB, not c#, so I either had to try to translate from an unfamiliar language, or learn one. – droopsnoot May 10 '20 at 18:38
  • With `XOAuth` you created the first token and with this one you call the other api. Is it working? If the token expired, what are you doing? – Enrico May 10 '20 at 20:03
  • I create an access token and a refresh token with XOauth. The refresh token has an expiry, and my code checks that before accessing Xero. If it's expired, I call the refresh endpoint (as described in that video) and get a new pair of tokens, and a new expiry. I don't use the API now - I couldn't see how to refresh from the API, so once I'd got HTTP get and post working, there didn't seem any point adding complexity. – droopsnoot May 11 '20 at 08:47

1 Answers1

1

Honestly, it was quite a mess. I spent more than 2 weeks to understand how to call Xero. I created a post to explain step by step what I have to do.

There were a lot of issues:

  • understand the Xero documentation because it is not very clear
  • find the right values to put in the request
  • translate the Postman request in code
  • no Xero documentation for that, only a useless bunch of projects

It is quite long to explain everything but for example if you want to read an Organization the code with RestSharp is:

/// <summary>
/// Gets the organizations.
/// </summary>
/// <returns>OrganizationResponse.</returns>
public OrganizationResponse GetOrganizations()
{
    var _client = new RestClient("https://api.xero.com/connections");
    _client.Timeout = -1;

    var request = new RestRequest(Method.GET);
    request.AddHeader("Authorization", "Bearer " + _settings.AccessToken);
    var response = _client.Execute<IList<Organization>>(request);

    return response;
}

I'll create other posts to explain how to implement more functions.

I created a NuGet package for that. Source code on Github.

Enrico
  • 3,592
  • 6
  • 45
  • 102