<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eclipsed4utoo&#039;s Blog&#187; REST</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsed4utoo.com/blog</link>
	<description>Not Your Ordinary Programmer</description>
	<lastBuildDate>Thu, 08 Sep 2011 17:09:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>.Net &#8211; Twitter Desktop OAuth Authorization</title>
		<link>http://eclipsed4utoo.com/blog/net-twitter-desktop-oauth-authentication/</link>
		<comments>http://eclipsed4utoo.com/blog/net-twitter-desktop-oauth-authentication/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 14:25:12 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=177</guid>
		<description><![CDATA[I recent finished the implementation of OAuth into my .Net Twitter library.  I can safely say that it was a pain in the ass.  I have never been so frustrated by &#8220;(401) Unauthorized&#8221; errors.  But alas, I was able to complete the implementation. As a guideline, I used Shannon Whitley&#8217;s code, since that&#8217;s the only [...]]]></description>
			<content:encoded><![CDATA[<p>I recent finished the implementation of OAuth into my .Net Twitter library.  I can safely say that it was a pain in the ass.  I have never been so frustrated by &#8220;(401) Unauthorized&#8221; errors.  But alas, I was able to complete the implementation.</p>
<p>As a guideline, I used <a href="http://www.voiceoftech.com/swhitley/?p=681">Shannon Whitley&#8217;s</a> code, since that&#8217;s the only .Net reference Twitter gives for OAuth implementation.  Since I had already created my own Twitter library that consumed 95% of the current Twitter REST API methods, I couldn&#8217;t simply add his two code files to my project and run with it.  I had to change some stuff around to get it to work with mine.  I also made some modifications to his class that makes all of the OAuth implementations.  In my opinion, it had a lot of code that didn&#8217;t need to be there.  I also created another class to consume the parameters needed for the OAuth requests so that I wasn&#8217;t passing 9 parameters to different methods.</p>
<p>So first, I want to explain OAuth a little and how it works for a desktop Twitter client.  I stress desktop application because Twitter has different implementations for web clients vs. desktop clients.</p>
<p>So <a href="http://oauth.net/">OAuth</a> is an open protocol to allow secure authorization to an API.  This means that for Twitter, the user&#8217;s of a desktop client aren&#8217;t required to give their username and password to the client.  The client can use OAuth to communicate with with the Twitter API without needing usernames and passwords.</p>
<p>Now for the process.  This was the toughest part for me to get my head around.  I couldn&#8217;t really find any good explanations of each step of the process.  So I will explain as much as I can.</p>
<p>Your first step is to <a href="http://twitter.com/oauth_clients">register your application</a> with Twitter.  This does a couple of things.  It gives you a &#8220;Consumer Key&#8221; and &#8220;Consumer Secret&#8221;.  These are needed in each OAuth request that is made.  This also provides the &#8220;source&#8221; for the tweets.  Twitter recently removed the ability to set the &#8220;source&#8221; of the tweet to your application name when using Basic Authentication.  This is now only allowed when using OAuth.</p>
<p>When making requests to OAuth methods, there are a number of parameters that are required for all requests.  And depending on the request, some may be expecting additional parameters.  The required parameters for all requests are:</p>
<p>1.  Consumer Key &#8211; given when you register your application.<br />
2.  Nonce &#8211; a random string.  I use a GUID.  Some use a random number.<br />
3.  Timestamp &#8211; the number of seconds since January 1, 1970.<br />
4.  Signature &#8211; an HMAC-SHA1 string of all of the other parameters.<br />
5.  Signature Method &#8211; HMAC-SHA1.  Currently Twitter only supports HMAC-SHA1.<br />
6.  Token &#8211; unauthorized/request/access token.<br />
7.  Verifier &#8211; the PIN from the Desktop Workflow<br />
8.  Version &#8211; &#8220;1.0&#8243;</p>
<p>When creating the signature, you will use the Consumer Secret and Token Secret as the key.  Also, the parameters that are part of the signature need to be in order when creating the signature and when making the request.  The signature is appended to the end of the parameters so it does not need to be in order.</p>
<p>Here is the order that the parameters need to be in:</p>
<p>1.  oauth_consumer_key<br />
2.  oauth_nonce<br />
3.  oauth_signature_method<br />
4.  oauth_timestamp<br />
5.  oauth_token<br />
6.  oauth_verifier<br />
7.  oauth_version<br />
8.  oauth_signature</p>
<p>To make the signature, you would use this code:</p>
<pre class="brush: csharp;">
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format(&quot;{0}&amp;{1}&quot;, UrlEncode(oauth.CustomerSecret), string.IsNullOrEmpty(oauth.TokenSecret) ? &quot;&quot; : UrlEncode(oauth.TokenSecret)));
// code to compute the hash to return as the signature
</pre>
<p>Moving to the code, your first step is to send an HttpWebRequest to http://twitter.com/oauth/request_token .  This request will return an Unauthorized Token and Unauthorized Token Secret.  In the current library that I used(and my Twitter API library), this method will return the entire URL including the token and secret, which will look like this&#8230;</p>
<p>http://twitter.com/oauth/authorize?oauth_token=UnauthorizedTokenValue</p>
<p>Your next step is to have the user go to the URL that was returned from the previous step.  This is the step that many developers have complained about.  When navigating to this URL, the user will enter their username and password.  They will then be given a PIN.  You will need to get this PIN from the user because it is required for you to get an Access Token.  I simply give a textbox for the user to copy and paste to.</p>
<p>After receiving the PIN, you will now need to make a request to http://twitter.com/oauth/access_token .  This will give you the Access Token that you will need to make all other requests to the Twitter REST API.  You will need to save these somewhere, whether it be in a database or in a file.</p>
<p>Here is some client code..</p>
<pre class="brush: csharp;">
TwitEclipseAPI twit = new TwitEclipseAPI();
twit.OAuthConsumerKey = &quot;&quot;; // your consumer key
twit.OAuthConsumerSecret = &quot;&quot;; // your consumer secret

// Once you get the access token, this
//    would be out to set it
//twit.OAuthAccessToken = &quot;&quot;;
//twit.OAuthAccessTokenSecret = &quot;&quot;;

string redirectURL = twit.OAuthGetUnauthorizedRequestToken();
Process.Start(redirectURL);

// show a popup to enter PIN
frmEnterPIN f = new frmEnterPIN();
f.ShowDialog();
string pin = f.PIN;
f.Dispose();

if (twit.OAuthRequestAccessToken(pin))
{
   twit.UpdateUserStatus(&quot;This is a test&quot;);
}
</pre>
<p>I have posted a link to the code files for my library and how it uses the edited version of Shannon&#8217;s library.</p>
<p>Post any questions in the comments.  I will answer them as best I can.  If  you run into any issues, please let me know.  Thanks.</p>
<p><strong>UPDATE:</strong> I have update the site with a new version of my twitter library.  I have also moved the download to CodePlex so that it will be easier for me to update.  <a href="http://twiteclipseapi.codeplex.com/" target="_blank">Here is the link</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/net-twitter-desktop-oauth-authentication/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

