This article mainly introduces the integration process with Xero OAuth, and the integration process with other third parties will be similar later. In addition, because Xero official documents are very limited, it is necessary to summarize.
Xero is a financial system that can be used to replace the implementation of a product billing module.
Www.xero.com
Learn about OAuth:
Http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
Http://baike.baidu.com/view/6619164.htm
In summary, for Systema users to access SYSTEMB resources, SYSB users can specify which parts of the SYSB (modules) are exposed to SYSA for access, while giving Sysa a token and certificate. You will need to bring tokens and certificates each time Sysa accesses SYSB.
1. Install NuGet pkg:
2. Create Wapper to encapsulate the interactive process
Premise:
1. Need to have the certificate installed
2. has been Key/secret
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; <summary>/////</summary>/<param name= "parameter" ></param> Public xeroapiadapter (ixeroapiparameter parameter) {_signingcertificate = XeroOAuthSettings.Fetch.Sign Ingcertificate.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 partnermvcauthent Icator (Partner_url, Base_url, CallBack, Xerotokenservices.do, _signingcertificate, _partnercertificate, New Consumer (_parameter. Consumerkey, _parameter. Consumersecret), xerorequesttokenservices.do); } }
RuPartnerAuthethicator.cs (primarily for the over write Xero de authorizeuser function, which opens the browser by default):
public class Rupartnerauthethicator:partnerauthenticator {public rupartnerauthethicator (String BaseUri, String Authorizeuri, Itokenstore Store, String Signingcertificatepath, String Certificatepath, String password): Base (BA Seuri, 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. Implements the IToken interface, which is divided into request token and Access token. That is, request token and access token, access token needs to be persisted, request token can be stored in memory.
public class Xerotokenservices:mongoservice, Itokenstore {public static xerotokenservices do { get {return new xerotokenservices (); }} private Xerotokenservices () {} private mongocollection<mdxerotoken> Xerotoke Nstore {get {return connection.getmongocollection<mdxerotoken> ("Xeroto Kenstore "); }} public void Add (IToken token) {//lets Delete first as we is not sure if Xero has a Delete delete (token); Xerotokenstore.save (new Mdxerotoken (token)); } public void Delete (IToken token) {xerotokenstore.remove (query<mdxerotoken>. EQ (x = X.userid, token. USERID)); Public IToken Find (string user) {var token = Xerotokenstore.findone (query<mdxerotoken>.e Q (x = x.userid, user)); return token; } public void Cleartokenfornetwork (string id) {xerotokenstore.remove (query<mdxerotoken>. EQ (x = X.userid, id)); }} public class Xerorequesttokenservices:mongoservice, Itokenstore {public static Xerorequesttokenserv Ices do {get {return new xerorequesttokenservices ();} Private Xerorequesttokenservices () {} private mongocollection<mdxerotoken> Xerotokens Tore {get {return connection.getmongocollection<mdxerotoken> ("xerorequ Esttokenstore "); }} public void Add (IToken token) {//lets Delete first as we is not sure if Xero has a Delete delete (token); Xerotokenstore.save (new Mdxerotoken (token)); } public void Delete (IToken token) {xerotokenstore.remove (query<mdxerotoken>. EQ (x => X.userid, token. USERID)); Public IToken Find (string user) {return xerotokenstore.findone (QUERY<MDXEROTOKEN>. EQ (x = x.userid, user)); } public void Cleartokenfornetwork (string id) {xerotokenstore.remove (query<mdxerotoken>. EQ (x = X.userid, id)); } }
4. Specify the callback function, in Xero configuration callback domain
4.1 Adding application
4.2 Configuring call back domain, generating key, 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 toke n = 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); }
Xero official Git:
Https://github.com/XeroAPI/Xero-Net
These are the things that integrate with Xero OAuth using C # and more about topic.alibabacloud.com (www.php.cn)!