使用DotNetOpenAuth來實現有道雲筆記的授權

來源:互聯網
上載者:User

     最近由於公司的需求特別多,而且還不是一個項目的需求,幾個資料庫幾個VS來回切換。難免搞混搞亂,忘記。於是自己用WPF做了一個小工具:AgileToDo,一個待辦列表。本來使用sqlce來做本地的儲存。但是在這個雲時代,搞本機存放區顯然不能拿出手。於是我決定給它整合有道雲筆記,使用有道雲筆記的的OPEN API來實現雲端的儲存。http://note.youdao.com/open/apidoc.html

     有道雲筆記的API使用OAuth的方式對第三方應用進行授權。OAuth的介紹就不多說了,說的人也挺多了,自己查資料吧。要使用它的API,我們需要授權。本來想完全有自己去實現一下這個過程的,不過看了授權的整個過程發現還是很複雜的,不花點時間估計還真搞不出來。於是用nuget搜一下發現了DotNetOpenAuth,而且這個類庫的排名是很靠前的,想必是個神器。這裡簡單說一下DotNetOpenAuth,據我簡單瞭解,DotNetOpenAuth主要是分了2部分的功能。一部分類庫是作為消費者,訪問第三方OAUTH服務,請求授權,比如我現在要做的事,訪問有道雲筆記的OAUTH服務。還有一部分類庫是用做服務方,實現OAUTH服務提供者的功能,可以使你的使用者系統支援OAUTH,供第三方消費者來使用。要深入瞭解的請直接去下載源碼,而且裡麵包含了很多sample,有web的有desktop的,訪問google,twitter的例子等等等。My Code當然也是參考了這些sample。

      經過一晚上的折騰,終於用.NET實現了有道雲筆記的授權。廢話不多說,上代碼吧。

1.添加DotNetOpenAuth的引用

2.YDAuthBaseInfo類

    /// <summary>    /// OAUTH授權所需的一些基礎資訊    /// </summary>    public class YDAuthBaseInfo    {        public static readonly string OwnerId = "";        public static readonly string ConsumerName = "";        public static readonly string ConsumerKey = "";//開發人員申請的KEY        public static readonly string ConsumerSecret = "";//開發人員申請的Secret        public static readonly string BaseUrl = "http://sandbox.note.youdao.com";//測試沙箱基礎url        public static readonly ServiceProviderDescription ServiceDescription = null;//OAUTH服務提供者資訊        static YDAuthBaseInfo()        {            OwnerId = "kklldog";            ConsumerName = "AgileToDo";            ConsumerKey = "xxxx";            ConsumerSecret = "xxxx";            ServiceDescription = new ServiceProviderDescription            {                RequestTokenEndpoint = new MessageReceivingEndpoint(YDAuthBaseInfo.BaseUrl + "/oauth/request_token",                    HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),                UserAuthorizationEndpoint = new MessageReceivingEndpoint(YDAuthBaseInfo.BaseUrl + "/oauth/authorize",                    HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),                AccessTokenEndpoint = new MessageReceivingEndpoint(YDAuthBaseInfo.BaseUrl + "/oauth/access_token",                     HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),                TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },            };        }    }

3.YDTokenManager 類

    /// <summary>    /// TokenManager 令牌管理    /// </summary>    public class YDTokenManager : IConsumerTokenManager    {        private Dictionary<string, string> _tokensAndSecrets = new Dictionary<string, string>();        private TokenType _tokenType;        /// <summary>        /// Initializes a new instance of the <see cref="YDTokenManager"/> class.        /// </summary>        /// <param name="consumerKey">The consumer key.</param>        /// <param name="consumerSecret">The consumer secret.</param>        public YDTokenManager(string consumerKey, string consumerSecret)        {            if (string.IsNullOrEmpty(consumerKey))            {                throw new ArgumentNullException("consumerKey");            }            this.ConsumerKey = consumerKey;            this.ConsumerSecret = consumerSecret;        }        /// <summary>        /// Gets the consumer key.        /// </summary>        /// <value>The consumer key.</value>        public string ConsumerKey { get; private set; }        /// <summary>        /// Gets the consumer secret.        /// </summary>        /// <value>The consumer secret.</value>        public string ConsumerSecret { get; private set; }        #region ITokenManager Members        public string GetTokenSecret(string token)        {            return this._tokensAndSecrets[token];        }         public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response)        {            this._tokensAndSecrets[response.Token] = response.TokenSecret;            _tokenType = TokenType.RequestToken;        }        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)        {            this._tokensAndSecrets.Remove(requestToken);            this._tokensAndSecrets[accessToken] = accessTokenSecret;            _tokenType = TokenType.AccessToken;        }        /// <summary>        /// Classifies a token as a request token or an access token.        /// </summary>        /// <param name="token">The token to classify.</param>        /// <returns>Request or Access token, or invalid if the token is not recognized.</returns>        public TokenType GetTokenType(string token)        {            return _tokenType;        }        #endregion    }
 
4.YDWebConsumer類
    /// <summary>    /// 有道OPEN AUTH的web端消費者實現    /// </summary>    public class YDWebConsumer:WebConsumer    {        public YDWebConsumer(ServiceProviderDescription serviceProvider, IConsumerTokenManager tokenManager)            : base(serviceProvider, tokenManager)        {        }        /// <summary>        /// 請求授權        /// </summary>        /// <param name="consumer"></param>        public static void RequestAuthorization(YDWebConsumer consumer)        {            if (consumer == null)            {                throw new ArgumentNullException("YDWebConsumer");            }            Uri callback = GetCallbackUrlFromContext();            var request = consumer.PrepareRequestUserAuthorization(callback, null, null);            consumer.Channel.Send(request);        }        /// <summary>        /// 擷取CALLBACKURL        /// </summary>        /// <returns></returns>        internal static Uri GetCallbackUrlFromContext()        {            Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_");            return callback;        }    }

5.有了這些我們就可以去請求授權了。讓我們來試試,建立一個ASP.NET項目,在Default.aspx下修改代碼

<%@ Page Title="首頁" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"    CodeBehind="Default.aspx.cs" Inherits="YDOpenAPI._Default" %><asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content><asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">    <h2>        歡迎使用 ASP.NET!    </h2>    <asp:Label runat="server" ID="lbl"></asp:Label>    </asp:Content>

 

        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                var youDao = new YDWebConsumer(YDAuthBaseInfo.ServiceDescription, this.TokenManager);                // 是否已經授權
                var accessTokenResponse = youDao.ProcessUserAuthorization();                if (accessTokenResponse != null)                {                    this.AccessToken = accessTokenResponse.AccessToken;                    this.lbl.Text ="Token:"+ this.AccessToken+" Screct:"+this.TokenManager.GetTokenSecret(this.AccessToken);                }                else if (this.AccessToken == null)                {                                        YDWebConsumer.RequestAuthorization(youDao);                }            }        }
 
//TokenManager    private YDTokenManager TokenManager        {            get            {                var tokenManager = (YDTokenManager)Session["tokenManager"];                if (tokenManager == null)                {                    string consumerKey = YDOpenAPI4N.YDAuthBaseInfo.ConsumerKey;                    string consumerSecret = YDOpenAPI4N.YDAuthBaseInfo.ConsumerSecret;                    if (!string.IsNullOrEmpty(consumerKey))                    {                        tokenManager = new YDTokenManager(consumerKey, consumerSecret);                        Session["tokenManager"] = tokenManager;                    }                }                return tokenManager;            }        }
 
跑一下是不是成功了。


有了AccessToken跟AccessSecret我們就可以去訪問有道雲筆記的操作API了:建立,刪除,修改筆記了。這部分接下來我也會實現。

      希望對於需要瞭解OAUTH跟DOTNETOPENAUTH的同學能有協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.