旗下导航:搜·么
当前位置:网站首页 > .Net教程 > 正文

运用C# 与Xero OAuth 交互 集成【C#.Net教程】,C#,Xero, OAuth

作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:88评论:0


导读:本文重要引见和xeroOAuth的集成历程,今后再与其他第三方的OAuth的集成历程也将相似。别的因为xero官方的文档异常有限,因此有必要举行总结一下。Xero是一个财务体...
本文重要引见和xero OAuth的集成历程,今后再与其他第三方的OAuth的集成历程也将相似。 别的因为xero官方的文档异常有限,因此有必要举行总结一下。
Xero 是一个财务体系,可用于替换产物账单模块的完成。
www.xero.com

相识OAuth :

http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html

http://baike.baidu.com/view/6619164.htm


归纳综合起来就是,关于SystemA用户要接见systemB的资本,sysB的用户能够指定将SysB中的哪些部份(模块)暴露出来给SysA举行接见,同时会给sysA一个token和证书。每次sysA接见sysB时须要带上token以及证书。

1. 装置 nuget pkg :


2. 建立wapper 来封装交互历程

条件:
1.须要已装置证书
2.已得到了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.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 (重要用于over write xero de AuthorizeUser函数,默许会翻开浏览器):

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. 完成IToken 接口,分为Request Token和 Access Token。 即要求token和接见token,接见token须要做耐久化,要求token可存内存中。

 public class XeroTokenServices : MongoService, ITokenStore
    {
        public static XeroTokenServices Do
        {
            get
            {
                return new XeroTokenServices();
            }


        }


        private XeroTokenServices()
        {


        }


        private MongoCollection<MDXeroToken> XeroTokenStore
        {
            get
            {
                return Connection.GetMongoCollection<MDXeroToken>("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<MDXeroToken>.EQ(x => x.UserId, token.UserId));
        }


        public IToken Find(string user)
        {
            var token = XeroTokenStore.FindOne(Query<MDXeroToken>.EQ(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 XeroRequestTokenServices Do
        {
            get { return new XeroRequestTokenServices(); }
        }


        private XeroRequestTokenServices()
        {


        }


        private MongoCollection<MDXeroToken> XeroTokenStore
        {
            get
            {
                return Connection.GetMongoCollection<MDXeroToken>("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<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. 指定callback 函数, 在xero设置callback domain

4.1 增加Application

4.2 设置call back domain , 生成key , secret

4.3 Call back 函数:

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);
        }

xero 官方 git:
https://github.com/XeroAPI/Xero-Net

以上就是运用C# 与Xero OAuth 交互 集成 的内容,更多相关内容请关注ki4网(www.ki4.cn)!

标签:C#XeroOAuth


欢迎 发表评论: