实现Evernote的OAuth授权

 

原理图

http://dev.evernote.com/media/images/oauth_large.png

第三方OAuth库

由于当中有一个签名的字段,需要实现一些比较复杂的操作。所以使用了danielcrenna的OAuth的库。下载地址https://github.com/danielcrenna/oauth

他里面的实例好像有点问题,好在库本身是可以使用的。而且非常方便。

代码

因为我是本地的程序所以我需要使用一个WebBrowser控件来完成认证授权的过程,另外也是因为是本地程序的原因我的回调地址就设置了一个localHost,这个回调地址必须有,不然会出现401错误,而且之后需要通过这个回调地址获取Verifier.这个回调地址,不一定是要可以访问的。

   1: private String ConsumerKey = "xxxxxx";
   2: private String ConsumerSecret = "xxxxxxyyyyyyy";
   3: private String RequestURI = "https://sandbox.evernote.com/oauth";
   4: private String AuthorizationURI = "https://sandbox.evernote.com/OAuth.action";
   5: private String CallbackURI = "http://localhost/EvernoteAdage";
   6: private String RequestToken;
   7: private String RequestTokenSecret;
   8: private String Verifier;
   9:  
  10: private string Token;
  11: private long Expires;
  12:  
  13: private void button2_Click(object sender, RoutedEventArgs e)
  14: {
  15:     RequesCredentials();
  16:     //使用webBrowser,导航到授权的网站
  17:     this.webBrowser1.Navigate(AuthorizationURI + "?oauth_token=" +
  18:                                                         RequestToken);
  19: }
  20:  
  21: //获取RequestToken和RequestTokenSecret
  22: private void RequesCredentials()
  23: {
  24:     //CallbackUrl是一定要设置的
  25:     OAuthRequest client = OAuthRequest.ForRequestToken(ConsumerKey,
  26:                                         ConsumerSecret);
  27:     client.RequestUrl = RequestURI;
  28:     client.CallbackUrl = CallbackURI;
  29:  
  30:     String auth = client.GetAuthorizationQuery();
  31:     String url = client.RequestUrl + "?" + auth;
  32:     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  33:  
  34:     //获取请求结果
  35:     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  36:     StreamReader reader = new StreamReader(response.GetResponseStream());
  37:     String result = reader.ReadToEnd();
  38:     reader.Close();
  39:     response.Close();
  40:  
  41:     //从结果中提取出RequestToken和RequestTokenSecret
  42:     RequestToken = result.Split('&')[0].Split('=')[1];
  43:     RequestTokenSecret = result.Split('&')[1].Split('=')[1];
  44: }
  45:  
  46: //针对授权的结果的Verifier
  47: private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
  48: {
  49:     if(e.Uri.ToString().StartsWith("http://localhost/EvernoteAdage"))
  50:     {
  51:         var splitted = e.Uri.ToString().Split('&').Select(s => s.Split('=')).ToDictionary(s => s.First(), s => s.Last());
  52:         if(!splitted.ContainsKey("oauth_verifier"))
  53:         {
  54:             return;
  55:         }
  56:         Verifier = splitted["oauth_verifier"];
  57:         //获取最终的Token
  58:         AccessCredentials();
  59:  
  60:         //显示结果
  61:         this.textBlock.Text =
  62:             String.Format("Token = {0}\nExpires = {1}",
  63:                            Token, Expires);
  64:         //进行退出处理
  65:         this.webBrowser1.Visibility = Visibility.Hidden;
  66:     }
  67: }
  68:  
  69: //获取访问所需的Token Expires
  70: private void AccessCredentials()
  71: {
  72:     OAuthRequest Client = OAuthRequest.ForAccessToken(ConsumerKey,
  73:                                                       ConsumerSecret,
  74:                                                       RequestToken,
  75:                                                       RequestTokenSecret,
  76:                                                       Verifier);
  77:     Client.RequestUrl = RequestURI;
  78:  
  79:     String auth = Client.GetAuthorizationQuery();
  80:     String url = Client.RequestUrl + "?" + auth;
  81:     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  82:  
  83:     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  84:     StreamReader reader = new StreamReader(response.GetResponseStream());
  85:     String result = reader.ReadToEnd();
  86:     reader.Close();
  87:     response.Close();
  88:  
  89:     var splitted = result.Split('&').Select(s => s.Split('=')).ToDictionary(s => s.First(), s => s.Last());
  90:     String deToken = splitted["oauth_token"];
  91:     String expiresStr = splitted["edam_expires"];
  92:  
  93:     Token = HttpUtility.UrlDecode(deToken);
  94:     Expires = Convert.ToInt64(expiresStr);
  95: }
posted @ 2012-10-01 13:20  atskyline  阅读(1665)  评论(2编辑  收藏  举报