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.
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