Scala OAuth Works!by Justin Michalicek on Aug. 17, 2011, 5:15 a.m. UTC
My Scala OAuth client more or less works now. I have sent HMAC-SHA1 and Plaintext signed token and access requests to both Twitter and Yahoo APIs using both GET and POST. I have also sent tweets to twitter, received my timeline, etc.
My primary goal was for a very simple to use library rather than one that does everything. I think I've more or less accomplished that. Only two objects are needed, one for the initial token request and access request and then the second for making signed requests. Currently only POST and GET are supported and only text/string responses. I may expand the library to work with non-text responses by returning lists of bytes rather than strings, but as it currently stands it does what probably the majority of OAuth clients need.
One thing I really am curious about is multipart POST. I have not seen any reference to doing this, so I'm not sure if OAuth supports it. It seems like the sort of thing that should be feasible, though. I'm just not sure how the request signature would be generated since using the actual file attachment would not be wise but not including the attachment in the signature seems against the basic premise of OAuth where the full URL and all POST and query string parameters are included in the request signature.
I'll update this in a day or two with a usage example. For now, here's the read only gitweb link. This may end up on github as well.
Here's the quick usage example. I used OOB rather than a callback URL since I was just testing with a local app.
import jm.oauth.OAuth import jm.oauth.Requester //Getting the request token val oauth = new OAuth(OAuth.POST, oauthConsumerSecret, oauthConsumerKey, OAuth.HMAC_SHA1) val requestToken = oauth.generateRequestToken(oauth_api_url, OAuth.OOB) //careful, different OAuth services return slightly different name/value pairs. println("request_token is " + requestToken) println(requestToken("oauth_token_secret")) //Getting the access token val oauth = new OAuth(OAuth.POST, oauthConsumerSecret, oauthConsumerKey, OAuth.HMAC_SHA1) val requestToken = oauth.generateRequestToken(oauth_api_url, OAuth.OOB) println("request_token is " + requestToken) println(requestToken("oauth_token_secret")) //Make a request - posting an updated to Twitter here val requester = new Requester(OAuth.HMAC_SHA1, TWITTER_CONSUMER_SECRET, TWITTER_CONSUMER_KEY, TWITTER_AUTHORIZED_TOKEN, TWITTER_AUTHORIZED_TOKEN_SECRET) val postMessage = Map("status" -> "another test") val response = requester.post("http://api.twitter.com/1/statuses/update.json", postMessage)