Recently, due to a lot of requirements from the company, and it is not a project requirement, several databases switch back and forth with. It's hard to confuse and forget. So I made a small tool using WPF: agiletodo, a to-do list. Originally, sqlce was used for local storage. However, in this cloud era, local storage cannot be used. So I decided to integrate youdao cloud notes and use the open API of youdao cloud notes to implement cloud storage. Http://note.youdao.com/open/apidoc.html
Youdao cloud note API uses oauth to authorize third-party applications. I will not talk much about oauth. There are too many people talking about it. Check your own information. To use its API, we need authorization. I wanted to implement this process on my own, but I found the entire process of authorization is still very complicated. I can't figure it out without a little time. So I searched and found dotnetopenauth with nuget, and the ranking of this class library is very high. It must be an artifact. Here is a brief introduction to dotnetopenauth. As far as I know, dotnetopenauth mainly consists of two functions. Some class libraries are used as consumers to access the third-party oauth service and request authorization. For example, what I want to do now, visit the oauth service of youdao cloud note. Another part of the class libraries are used as service providers to implement the oauth service provider function. This allows your user system to support oauth for third-party consumers. For more information, download the source code, including a lot of samples, Web desktop, Google, Twitter examples, and so on. Of course, my code also references these samples.
After one night of hard work, I finally used. Net to authorize youdao cloud notes. Not much nonsense.CodeRight.
1. Add dotnetopenauth reference
2. ydauthbaseinfo class
/// <Summary> /// Some basic information required for oauth authorization /// </Summary> Public Class Ydauthbaseinfo { Public Static Readonly String Ownerid = "" ; Public Static Readonly String Consumername = "" ; Public Static Readonly String Consumerkey = "" ; // Key applied by the developer Public Static Readonly String Consumersecret = "" ; // Secret applied by the developer Public Static Readonly String Baseurl = Http://sandbox.note.youdao.com" ; // Test the sandbox URL Public Static Readonly Serviceproviderdescription servicedescription = Null ; // Oauth service provider Information 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 class
/// <Summary> /// Tokenmanager token Management /// </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 Expirerequesttokenandstorenewaccesen en ( 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 class
/// <Summary> /// Web consumer implementation with youdao open auth /// </Summary> Public Class Ydwebconsumer: webconsumer { Public Ydwebconsumer (serviceproviderdescription serviceprovider, iconsumertokenmanager tokenmanager ): Base (Serviceprovider, tokenmanager ){} /// <Summary> /// Request authorization /// </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> /// Obtain callbackurl /// </Summary> /// <Returns> </returns> Internal Static Uri getcallbackurlfromcontext () {URI callback = messagingutilities. getrequesturlfromcontext (). stripqueryargumentswithprefix ( "Oauth _" );Return Callback ;}}
5. With this, we can request authorization. Let's try to create an ASP. Net project and modify the code Under default. aspx.
<% @ Page title = "Homepage" 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 > Welcome to 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); // whether authorization has been performed
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 ;}}
Check whether the task is successful.
With accesstoken and accesssecret, we can access the operation API of youdao cloud notes: Create, delete, and modify notes. I will also implement this part.
I hope this will be helpful for those who need to know about oauth and dotnetopenauth.