Use C # to interact with Xero OAuth
This article mainly introduces the integration process with xero OAuth. The integration process with other third-party OAuth will be similar later. In addition, the xero official documentation is very limited, so it is necessary to summarize it.
Xero is a financial system that can be used to replace the implementation of the product billing module.
Www.xero.com
Learn about OAuth:
In summary, sysB users can specify which parts (modules) of SysB are exposed to SysA for access to systemB resources, at the same time, a token and certificate will be given to sysA. Each time sysA accesses sysB, a token and a certificate are required.
1. Install nuget pkg:
2. Create a wapper to encapsulate the interaction process
Prerequisites:
1. You must have installed the certificate.
2. The key/secret has been obtained.
public class XeroApiAdapter { private readonly IXeroApiParameter _parameter; private const string PARTNER_URL = https://api-partner.network.xero.com; private const string BASE_URL = https://api.xero.com; public XeroCoreApi CoreApi { get; private set; } private readonly X509Certificate2 _signingCertificate; private readonly X509Certificate2 _partnerCertificate; /// /// /// /// public XeroApiAdapter(IXeroApiParameter parameter) { _signingCertificate = XeroOAuthSettings.Fetch.SigningCertificate.SelectedCertificate; if (_signingCertificate == null) { throw new Exception(Signing certificate must be defined); } _partnerCertificate = XeroOAuthSettings.Fetch.PartnerCertificate.SelectedCertificate; if (_partnerCertificate == null) { throw new Exception(partner certificate must be defined); } _parameter = parameter; var user = new ApiUser { OrganisationId = parameter.NetworkId, Name = parameter.NetworkId }; CoreApi = new XeroCoreApi(PARTNER_URL, new RuPartnerAuthethicator(PARTNER_URL, BASE_URL, XeroTokenServices.Do, _signingCertificate, _partnerCertificate), new Consumer(parameter.ConsumerKey, parameter.ConsumerSecret), user, new DefaultMapper(), new DefaultMapper()); } public PartnerMvcAuthenticator MvcAuthenticator(string callBack) { return new PartnerMvcAuthenticator(PARTNER_URL, BASE_URL, callBack, XeroTokenServices.Do, _signingCertificate, _partnerCertificate, new Consumer(_parameter.ConsumerKey, _parameter.ConsumerSecret), XeroRequestTokenServices.Do); } }
RuPartnerAuthethicator. cs (mainly used for the over write xero de AuthorizeUser function. The browser is opened by default ):
public class RuPartnerAuthethicator : PartnerAuthenticator { public RuPartnerAuthethicator(string baseUri, string authorizeUri, ITokenStore store, string signingCertificatePath, string certificatePath, string password) : base(baseUri, authorizeUri, , store, signingCertificatePath, certificatePath, password) { } public RuPartnerAuthethicator(string baseUri, string authorizeUri, ITokenStore store, X509Certificate2 signingCertificate, X509Certificate2 certificate) : base(baseUri, authorizeUri, , store, signingCertificate, certificate) { } protected override string AuthorizeUser(IToken token) { throw new XeroRenewAccessTokenException(Please renew access token); } }
3. Implement the IToken interface, which can be divided into Request Token and Access Token. That is, to request token and to access token, the access token must be persistent, and the request token can be stored in memory.
public class XeroTokenServices : MongoService, ITokenStore { public static XeroTokenServices Do { get { return new XeroTokenServices(); } } private XeroTokenServices() { } private MongoCollection
XeroTokenStore { get { return Connection.GetMongoCollection
(XeroTokenStore); } } public void Add(IToken token) { //Lets delete first as we are not sure if Xero have a delete Delete(token); XeroTokenStore.Save(new MDXeroToken(token)); } public void Delete(IToken token) { XeroTokenStore.Remove(Query
.EQ(x => x.UserId, token.UserId)); } public IToken Find(string user) { var token = XeroTokenStore.FindOne(Query
.EQ(x => x.UserId, user)); return token; } public void ClearTokenForNetwork(string id) { XeroTokenStore.Remove(Query
.EQ(x => x.UserId, id)); } } public class XeroRequestTokenServices : MongoService, ITokenStore { public static XeroRequestTokenServices Do { get { return new XeroRequestTokenServices(); } } private XeroRequestTokenServices() { } private MongoCollection
XeroTokenStore { get { return Connection.GetMongoCollection
(XeroRequestTokenStore); } } public void Add(IToken token) { //Lets delete first as we are not sure if Xero have a delete Delete(token); XeroTokenStore.Save(new MDXeroToken(token)); } public void Delete(IToken token) { XeroTokenStore.Remove(Query
.EQ(x => x.UserId, token.UserId)); } public IToken Find(string user) { return XeroTokenStore.FindOne(Query
.EQ(x => x.UserId, user)); } public void ClearTokenForNetwork(string id) { XeroTokenStore.Remove(Query
.EQ(x => x.UserId, id)); } }
4. Specify the callback function and configure the callback domain in xero.
4.1 Add Application
4.2 configure call back domain to generate key and secret
4.3 Call back function:
public ActionResult Authorize(string oauth_token, string oauth_verifier, string org, string redirect) { var network = NetworksManagment.Do.GetNetwork(Tenant.NetworkId); var xeroApi = new XeroApiAdapter(new XeroApiParam(network)); var authenthicator = xeroApi.MvcAuthenticator(); try { // - call XeroTokenServices.Add and store the token in MDXeroToken var token = authenthicator.RetrieveAndStoreAccessToken(network.Id, oauth_token, oauth_verifier, org); var organization = xeroApi.CoreApi.Organisation; ... TempData.AddNotification(NotifcationType.Success, Xero connected successfully); } catch (Exception ex) { TempData.AddNotification(Error connecting to Xero, ex); } if (string.IsNullOrEmpty(redirect)) { return RedirectToAction(Index); } return Redirect(redirect); }