1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Net;
  11. using System.IO;
  12. using System.Collections.Specialized;
  13. namespace oAuthExample
  14. {
  15. public class oAuthTumblr : OAuthBase
  16. {
  17. public enum Method { GET, POST };
  18. public const string REQUEST_TOKEN = "http://www.tumblr.com/oauth/request_token";
  19. public const string AUTHORIZE = "http://www.tumblr.com/oauth/authorize";
  20. public const string ACCESS_TOKEN = "http://www.tumblr.com/oauth/access_token";
  21. public const string XAUTH_ACCESS_TOKEN = "https://www.tumblr.com/oauth/access_token";
  22. private string _consumerKey = "";
  23. private string _consumerSecret = "";
  24. private string _token = "";
  25. private string _tokenSecret = "";
  26. private string _verifier = "";
  27. private string _xAuthUsername = "";
  28. private string _xAuthPassword = "";
  29. /// <summary>
  30. /// Get the link to Tumblr's authorization page for this application.
  31. /// </summary>
  32. /// <returns>The url with a valid request token, or a null string.</returns>
  33. public string AuthorizationLinkGet()
  34. {
  35. string ret = null;
  36. string response = oAuthWebRequest(Method.GET, REQUEST_TOKEN, String.Empty);
  37. if (response.Length > 0)
  38. {
  39. //response contains token and token secret. We only need the token.
  40. NameValueCollection qs = HttpUtility.ParseQueryString(response);
  41. if (qs["oauth_token"] != null)
  42. {
  43. ret = AUTHORIZE + "?oauth_token=" + qs["oauth_token"];
  44. }
  45. }
  46. return ret;
  47. }
  48. /// <summary>
  49. /// Exchange the request token for an access token.
  50. /// </summary>
  51. /// <param name="authToken">The oauth_token is supplied by </param>
  52. public void AccessTokenGet(string authToken, string verifier)
  53. {
  54. this.Token = authToken;
  55. this.Verifier = verifier;
  56. string response = oAuthWebRequest(Method.GET, ACCESS_TOKEN, String.Empty);
  57. if (response.Length > 0)
  58. {
  59. //Store the Token and Token Secret
  60. NameValueCollection qs = HttpUtility.ParseQueryString(response);
  61. if (qs["oauth_token"] != null)
  62. {
  63. this.Token = qs["oauth_token"];
  64. }
  65. if (qs["oauth_token_secret"] != null)
  66. {
  67. this.TokenSecret = qs["oauth_token_secret"];
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Exchange the username and password for an access token.
  73. /// </summary>
  74. /// <param name="username">Tumblr Username.</param>
  75. /// <param name="username">Tumblr Password.</param>
  76. public void xAuthAccessTokenGet(string username, string password)
  77. {
  78. this.xAuthUsername = username;
  79. this.xAuthPassword = password;
  80. string response = oAuthWebRequest(Method.GET, XAUTH_ACCESS_TOKEN, String.Empty);
  81. if (response.Length > 0)
  82. {
  83. //Store the Token and Token Secret
  84. NameValueCollection qs = HttpUtility.ParseQueryString(response);
  85. if (qs["oauth_token"] != null)
  86. {
  87. this.Token = qs["oauth_token"];
  88. }
  89. if (qs["oauth_token_secret"] != null)
  90. {
  91. this.TokenSecret = qs["oauth_token_secret"];
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Submit a web request using oAuth.
  97. /// </summary>
  98. /// <param name="method">GET or POST</param>
  99. /// <param name="url">The full url, including the querystring.</param>
  100. /// <param name="postData">Data to post (querystring format)</param>
  101. /// <returns>The web server response.</returns>
  102. public string oAuthWebRequest(Method method, string url, string postData)
  103. {
  104. string outUrl = "";
  105. string querystring = "";
  106. string ret = "";
  107. //Setup postData for signing.
  108. //Add the postData to the querystring.
  109. if (method == Method.POST)
  110. {
  111. if (postData.Length > 0)
  112. {
  113. //Decode the parameters and re-encode using the oAuth UrlEncode method.
  114. NameValueCollection qs = HttpUtility.ParseQueryString(postData);
  115. postData = "";
  116. foreach (string key in qs.AllKeys)
  117. {
  118. if (postData.Length > 0)
  119. {
  120. postData += "&";
  121. }
  122. qs[key] = HttpUtility.UrlDecode(qs[key]);
  123. qs[key] = this.UrlEncode(qs[key]);
  124. postData += key + "=" + qs[key];
  125. }
  126. if (url.IndexOf("?") > 0)
  127. {
  128. url += "&";
  129. }
  130. else
  131. {
  132. url += "?";
  133. }
  134. url += postData;
  135. }
  136. }
  137. Uri uri = new Uri(url);
  138. string nonce = this.GenerateNonce();
  139. string timeStamp = this.GenerateTimeStamp();
  140. //Generate Signature
  141. string sig = this.GenerateSignature(uri,
  142. this.ConsumerKey,
  143. this.ConsumerSecret,
  144. this.Token,
  145. this.TokenSecret,
  146. this.Verifier,
  147. this.xAuthUsername,
  148. this.xAuthPassword,
  149. method.ToString(),
  150. timeStamp,
  151. nonce,
  152. out outUrl,
  153. out querystring);
  154. querystring += "&oauth_signature=" + HttpUtility.UrlEncode(sig);
  155. //Convert the querystring to postData
  156. if (method == Method.POST)
  157. {
  158. postData = querystring;
  159. querystring = "";
  160. }
  161. if (querystring.Length > 0)
  162. {
  163. outUrl += "?";
  164. }
  165. ret = WebRequest(method, outUrl + querystring, postData);
  166. return ret;
  167. }
  168. /// <summary>
  169. /// Web Request Wrapper
  170. /// </summary>
  171. /// <param name="method">Http Method</param>
  172. /// <param name="url">Full url to the web resource</param>
  173. /// <param name="postData">Data to post in querystring format</param>
  174. /// <returns>The web server response.</returns>
  175. public string WebRequest(Method method, string url, string postData)
  176. {
  177. HttpWebRequest webRequest = null;
  178. StreamWriter requestWriter = null;
  179. string responseData = "";
  180. webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
  181. webRequest.Method = method.ToString();
  182. webRequest.ServicePoint.Expect100Continue = false;
  183. //webRequest.UserAgent = "Identify your application please.";
  184. //webRequest.Timeout = 20000;
  185. if (method == Method.POST)
  186. {
  187. webRequest.ContentType = "application/x-www-form-urlencoded";
  188. //POST the data.
  189. requestWriter = new StreamWriter(webRequest.GetRequestStream());
  190. try
  191. {
  192. requestWriter.Write(postData);
  193. }
  194. catch
  195. {
  196. throw;
  197. }
  198. finally
  199. {
  200. requestWriter.Close();
  201. requestWriter = null;
  202. }
  203. }
  204. responseData = WebResponseGet(webRequest);
  205. webRequest = null;
  206. return responseData;
  207. }
  208. /// <summary>
  209. /// Process the web response.
  210. /// </summary>
  211. /// <param name="webRequest">The request object.</param>
  212. /// <returns>The response data.</returns>
  213. public string WebResponseGet(HttpWebRequest webRequest)
  214. {
  215. StreamReader responseReader = null;
  216. string responseData = "";
  217. try
  218. {
  219. responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
  220. responseData = responseReader.ReadToEnd();
  221. }
  222. catch
  223. {
  224. throw;
  225. }
  226. finally
  227. {
  228. webRequest.GetResponse().GetResponseStream().Close();
  229. responseReader.Close();
  230. responseReader = null;
  231. }
  232. return responseData;
  233. }
  234. }
  235. }