Enrico
Enrico

Reputation: 6266

Xero Accounting Sdk Core: authentication error

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

Upvotes: 1

Views: 477

Answers (1)

Enrico
Enrico

Reputation: 6266

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.

Upvotes: 1

Related Questions