-
► Ҝėvȋń ◄ ♂


- Joined on 02-05-2008
- Posts 25
|
OAuth, getting it to work
No doubt there's a lot of confusion in getting OAuth to work; it doesn't help that you're probably debugging against a boolean output. Thought I'd share my fustrations and "tips" on how I was able to get it working, in the hopes of saving someone from jumping off a bridget :) Some of this is from other posts, some is original.
Cheers, Kevin
Helpful links:
Definitions:
-
Consumer Key - Application URI, looks like http://www.myspace.com/123456789 - but you need to url encode it so that it's recieved like: http%3A%2F%2Fwww.myspace.com%123456789
-
Consumer Secret - Security Key, looks like 079b72r9z9884e17b6504f723a179c23
-
OAuth Token - Is a request paramater, is indexed by "oauth_token" (ie, Request.params["oauth_token"]
-
OAuth Token Secret - this is an empty string, ""
Consume key & consumer secret are both found in the My Apps / App Details page. Never share the consumer secret key with anyone ;)
Pseudocode: (found in many formums)
string generatedStr = @"your generated string here"; (samples below) string key = UrlEncode(string.Format("{0}&{1}", UrlEncode(consumerSecret), UrlEncode(tokenSecret))); HashAlgorithm hashAlgorithm = new HMACSHA1(Encoding.UTF8.GetBytes(key)); string computedHash = Convert.ToBase64String(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(str)))); return computedHash.compareTo(sentHash) == 0;
Cryptography Basics
The goal is to compute a hash that matches the hash provided by the MySpace server...
Keep in mind that purpose of this execise (of signing the request) - to keep the middle man out. The spec allows no room for errors (even if it's not published correctly), and all generated strings are case senstivie. In the .Net platform, that means the default UrlEncode function actually generates the wrong case!! So, yes, your generated string & keys have to be exact...
Debugging:
- Remove as much abstraction as possible, including environment, etc. Start with the OAuth tool, which will allow you to work against a constant set of paramaters.
- use realistic values (yet keep them simple)
- Set the version to 1.0 (server does anyways)
- Take the string generated output (see sample base string & sample signature) and get it to work in your algorithm first!
Sample Base String: GET&http%3A%2F%2Fapi.myspace.com%2Fmystuff%2Fdynamic.aspx&oauth_consumer_key%3Dhttp%253A%252F%252Fwww.myspace.com%252F330069419%26oauth_nonce%3D987654321%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1205192397%26oauth_token%3DoauthToken%26oauth_version%3D1.0
Signature: azN0BCWSkoCyfDAMrl5JLpgSSac=
-
Pain points so far - generating the
EXACT base string as the OAuth tool!
-
The generated base string includes whitespace (none), and casing (may not be correct if using system libraries). This is by far the hardest part, so don't bother moving forward without being able to replicating the generated string.
-
I'd suggest using a tool like Beyond Compare (or other visual diff tool), where you split each line up by the = sign (encoded %26), so you can truly compare paramater by paramater. It has to be exact!
-
I also had to double encode the consumer key to match the OAuth Tool's base string.
-
Do not drop empty variables (ie, if a paramater is empty / null, you still need to include it)
-
The normalized request paramaters are as follows:
-
start with all org paramaters provide
-
remove all OAuth paramaters except those needed by myspace (needed: OAuthVersionKey, OAuthNonceKey, OAuthTimestampKey, OAuthSignatureMethodKey, OAuthConsumerKeyKey, OAuthTokenKey)
-
sort remaining paramaters alphabetically, numbers first, 0 before 1
-
Unconfirmed: "(" and ")" characters may need to be encoded, this is true for other cryto projects i've done in the past. I've seen forum posts stating as much.
-
Each portion of the string is url encoded; not the entire string:
-
Once you have a matching base string, now try to generate a signature. The pseudocode to generate your own signature is above. Keep a look out for encoding (UTF8).
-
A sample key should match the following format: "079b72r9z9884e17b6504f723a179c23&" (note the trailing "&")
-
If you don't have the OAuth signature, then you've already gone wrong; no need to consider other environmental problems. Repeat debugging until you get this far.
-
From here, it's time to move against the MySpace server. It shouldn't be too hard, other then each request will be different from the previous - that is, the timestamp will always be different, so you can no longer hard code anything; all variables must come from the request that originated at MySpace.
-
Also, consider that the OAuth tool, even with the same variables that the myspace server send, will generate a different signature. This is because there are extra paramaters, that can not be input into the OAuth tool (ie, viewer_id, owner_id, etc). So it's time to leave it behind :)
-
At this time, you're flying somewhat blind. But, as long as you can normalize your parameters properly, you should be OK... But:
-
Certain characters (?, &, (, ), %, etc) may cause problems - try them in the OAuth tool to see if you can get an exact match with your generated string & signature.
OK, that's it for now. Hopefully this thread will remain useful / grow with time. I can say, that OAuth is possible, but it's important to get the simple steps working first...
Good luck! ~Kevin
|
|