Lindsey Bieda
Caffeine Powered Automaton

OAuth with C# Part II: Authorize and Access Token

Continuing from Part I we need to now allow the user to authorize the application to access their data and we need to obtain our access token. The user authorization is as simple as appending the oauthToken to the end of the access URL and opening a browser window:

// go get authorized
string oauthURL = "http://www.goodreads.com/oauth/authorize?oauth_token=" + oauthToken;
// open a browser and allow the user to authorize
System.Diagnostics.Process.Start(oauthURL);


Once the user has verified that they have allowed your application access then you can obtain the access token and secret. This follows pretty much exactly from the Part I code except this time when generating the signature we want to pass in the OAuth token and OAuth token secret that we got in Part I.


uri = new Uri("http://www.goodreads.com/oauth/access_token");
nonce = oAuth.GenerateNonce();
timeStamp = oAuth.GenerateTimeStamp();
// this time we need our oauth token and oauth token secret
sig = oAuth.GenerateSignature(uri, consumerKey, consumerSecret, oauthToken, oauthTokenSecret, "GET", timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);
sig = HttpUtility.UrlEncode(sig);

// notice that the sig is always being appended to the end
string accessUrl = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + sig;

oauthTokenReq(accessUrl, out oauthToken, out oauthTokenSecret);
Console.WriteLine("ouathToken: " + oauthToken + "oauthTokenSecret:" + oauthTokenSecret);


Note that oauthTokenReq() is doing the same work of that ugly do while loop from Part I to grab the data from the page, but I decided to throw it into it's own method.

OAuth with C# Part I: Request Token

Using the C# code available on Google Code you can add OAuthBase.cs to a new or existing C# project. From that point you just need to add the following statements to the top of whatever file you wish to make the request from:

using System.Web;
using System.Net;
using OAuth;


You may need to add System.Web to the project References if it isn't already in the list. The following code will output the OAuth token and OAuth token secret which you will need in order to verify your application.

public void getOAuthToken()
        {
            // replace with your key and secret
            string consumerKey = "key";
            string consumerSecret = "secret"; // shhh! It's a secret

            // please note that the url needs to be EXACTLY the url it needs to / will hit
            // this means if the page forwards foo.bar to www.foo.bar the uri needs to be specified as www.foo.bar
            Uri uri = new Uri("http://www.goodreads.com/oauth/request_token");
            OAuthBase oAuth = new OAuthBase();
            
            string nonce = oAuth.GenerateNonce();
            string timeStamp = oAuth.GenerateTimeStamp();
            string normalizedUrl;
            string normalizedRequestParameters;
            string sig = oAuth.GenerateSignature(uri, consumerKey, consumerSecret, null, null, "GET", timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);

            sig = HttpUtility.UrlEncode(sig);

            string request_url = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + sig;

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(request_url);
            var responseStream = httpRequest.GetResponse().GetResponseStream();

            byte[] buf = new byte[1024];
            int count = 0;
            StringBuilder sb = new StringBuilder("");
            string tempString;

            do
            {
                // fill the buffer with data
                count = responseStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0);

            // parse out the tokens from the response
            string oauthToken = sb.ToString().Substring(12,sb.ToString().IndexOf('&')-12);
            string oauthTokenSecret = sb.ToString().Substring(sb.ToString().IndexOf('&') + 20);

            Console.WriteLine("token: " + oauthToken + " secret: " + oauthTokenSecret);

            System.Diagnostics.Debug.WriteLine(request_url);
        }

It's a lot of bullshit. Thanks Dave for noting that the hash is generated on the URL and the HTTP method.

The New Age

Come one come all for the new age of technology.
The digital download and the always abundant digital storefront.

We give you freedom. Freedom from porn. Freedom from free speech.
Freedom to hear and see what we want you to.

Paying is easier than ever.
Just hand over your credit card and we'll take care of the rest.

Sharing is not a right.
Ownership is not a right.
We dictate what you can and cannot do with the product, it's the only way to be safe.

Your digital rights are now our digital rights.
Your liberty is now our liberty.

For your convenience we have removed unnecessary features.
For your safety our stores will provide you with all the content you'll ever need.
Thinking is now optional.

Your books, your movies, your music remains our property.
We have liberated you from ownership.

We own the deed and dollar and download.

New Delicious Flavor

I just rolled out a new layout for the site and cleaned up pretty much everything. The code link now goes to my github account. Just seems to make more sense than hosting everything in two places. The about page now features feeds from twitter, github, steepster, and goodreads.

Please excuse any bugs you come across as I work out any kinks with the new design.

Ada Lovelace and Women in Computing Today

Ada Lovelace Ada Lovelace is considered the world's first computer programmer. In the world of computer science today it's almost astonishing that computer programming could have first been done by a woman. After all the percentage of woman in computing professions is somewhere around 20%. The reasons for this seem to be completely out of our grasp. It's been suggested that the “geek” stereotype is completely unappealing for women, but the number of female videogamers is around 38% and rising. Additionally, the percentage of female Star Trek fans is somewhere around 40% (which I think we can all agree is far geekier than programming).

There also is speculation that there is a factor of family influence. Of the few women I have talked to in the computer science field didn't start programming until college, and of them most had some family member who was involved in a computing profession. While I see this being a factor in which female students are aware of the field I can't see it as a contributor to very few women choosing computer science. After all, with all the men in the field the number of father's with CS degrees and a daughter should be fairly high.

I honestly don't have the slightest clue what is holding gender back from computer science. I got into it young. No one else in my family was a programmer or engineer. I started out with HTML and learned that I absolutely loved that I had the ability to create something. For an entire year before I wrote my first program I did nothing but read. I read about the hacker culture. I printed out the jargon file and read every bit. Finally, by a hacker's recommendation, I grabbed a C compiler and started teaching myself C (what an odd choice for a first language). Nothing held me back and I wasn't even aware of a gender gap until high school when I enrolled in my first programming class.

Later in college during the TLI program where I was teaching computer science, to primarily female, students I noticed something. Prior to writing their first programs the girls were apprehensive saying that “it will be hard” and “it's gonna be boring”, but once they wrote their first bit of code something changed. Suddenly, they were excited about code and starting to notice that even doing the smallest things felt awesome. I don't know how many of those students are going off to pursue computer science degrees, but at least they found something enjoyable in the experience.

I propose that the number of women in computer science could be aided by simply exposing younger girls to programming at a younger age. I call it the programmer's high; when you finish a good piece of code and it works perfectly and you feel ecstatic. You made something. Those two hands of yours reached out into the world and created something that wasn't there before. As the cigarette companies will tell you: “You gotta hook 'em young.”

References