Xero Public Application Integration With C#

955 views Asked by At

I want to integrate xero with c# windows service application. I did not find a simple code snippet to connect xero with c#. I don't want any user interaction while authorising the user with xero.

I found the code below but it redirects me to xero login page to authenticate and then generates the verification code, how can I avoid this and go ahead because in windows service I will not have any gui to enter verification code.

using System;
using System.Linq;
using System.Windows.Forms;
using Xero.Api.Core;
using Xero.Api.Example.Applications.Public;
using Xero.Api.Example.TokenStores;
using Xero.Api.Infrastructure.OAuth;
using Xero.Api.Serialization;


namespace XeroIntegrationTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ConnectXero();
        }

        public void ConnectXero()
        {
            try
            {
                // Public Application Sample
                var user = new ApiUser { Name = Environment.MachineName };
                string consumerKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                string consumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

                var public_app_api = new XeroCoreApi("https://api.xero.com/api.xro/2.0/", new PublicAuthenticator("https://api.xero.com/api.xro/2.0/", "https://api.xero.com/oauth/RequestToken", "oob",
                    new MemoryTokenStore()),
                    new Consumer(consumerKey, consumerSecret), user,
                    new DefaultMapper(), new DefaultMapper());


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

            } catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
    }
}

But it generates oauth_problem=permission_denied&oauth_problem_advice=The%20consumer%20was%20denied%20access%20to%20this%20resource

error.

Hope someone will help me.

2

There are 2 answers

0
Gopal Palraj On

You need to integrate with "privateKeyAuthendicator" method.

To do this :

1.Create Private/Public Key using below link

https://developer.xero.com/documentation/api-guides/create-publicprivate-key

2.Create ConsumerKey and ConsumerSecret

3.Include Key file into your project folder

4.Use below code snippet to access Xero

var private_app_api = new XeroCoreApi(_xeroSettings.Value.Url, new PrivateAuthenticator(privatekeyFilePath, privatekeypassword),
                new Consumer(consumerKey,consumerSecret), null,
                new DefaultMapper(), new DefaultMapper());
0
justanotherdev On

You need to integrate using the "PrivateAuthenticator" method. The method you are using is the public one and uses a different authentication process which is not suitable for a windows service.

To do this:

  1. Visit the Xero developer portal
  2. Go to the "My Apps" section
  3. Select "Add Application"
  4. Select "Private" from the radio button options (this is important)
  5. Generate a Private/Public key combination (google how to do this)
  6. Follow the code samples in the readme for setting up a connection using the PrivateAuthenticator method