public
Created — forked from tommorris/gist:437512

talking to oauth-out-of-band fireeagle using scala's databinder dispatch oauth

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// Welcome to Scala version 2.7.5.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
// Type in expressions to have them evaluated.
// Type :help for more information.
 
scala> import dispatch._
import dispatch._
 
scala> import oauth._
import oauth._
 
scala> import OAuth._
import OAuth._
 
scala> import scala.xml._
import scala.xml._
 
scala> val fe = (:/("fireeagle.yahooapis.com") / "oauth").secure
fe: dispatch.Request = dispatch.Request@65370ce9
 
scala> val oauth = Consumer("[seekrit!]", "[also seekrit!]")
oauth: dispatch.oauth.Consumer = Consumer([secret!],[secret!])
 
scala> val h = new Http
h: dispatch.Http = dispatch.Http@1045651b
 
scala> val request_token = h(fe / "request_token" << Map("oauth_callback" -> "oob") <@ oauth as_token)
// INF: [console logger] dispatch: POST https://fireeagle.yahooapis.com/oauth/request_token
request_token: dispatch.oauth.Token = Token([secret],[secret])
 
scala> request_token
res0: dispatch.oauth.Token = Token([secret],[secret])
 
scala> fe / "authorize" <<? request_token
res1: dispatch.Request = dispatch.Request@57ced290
 
scala> (fe / "authorize" <<? request_token).to_uri
res2: java.net.URI = https://fireeagle.yahooapis.com/oauth/authorize?oauth_token=[secret]
 
// At this point, the loyal user went off to the relevant URL and authenticated with the application.
// He returned with an OAuth verification token. What a nice user.
 
scala> val access_token = h(fesecure / "access_token" << Map("oauth_verifier" -> "KXfQgG") <@ (oauth, request_token) as_token)
// INF: [console logger] dispatch: POST https://fireeagle.yahooapis.com/oauth/access_token
access_token: dispatch.oauth.Token = Token([more secret],[omg secret])
 
scala> val feapi = (:/("fireeagle.yahooapis.com") / "api" / "0.1").secure
feapi: dispatch.Request = dispatch.Request@16dba1f7
 
scala> val location = XML.loadString(h(feapi / "user" <@ (oauth, access_token) as_str))
// INF: [console logger] dispatch: GET https://fireeagle.yahooapis.com/api/0.1/user
location: scala.xml.Elem =
<rsp stat="ok" xmlns:georss="http://www.georss.org/georss">
<user token="azfpzT5ah3Ol" located-at="2010-06-14T02:20:47-07:00" readable="true" writable="true">
<location-hierarchy timezone="Europe/London" string="23424975|24554868|12602173|26788690|26352313">
<location best-guess="true">
<georss:point>51.130219 0.262814</georss:point>
...
 
scala> location \\ "location"
res4: scala.xml.NodeSeq =
<location best-guess="true" xmlns:georss="http://www.georss.org/georss">
<georss:point>51.130219 0.262814</georss:point>
<label></label>
<level>0</level>
<level-name>exact</level-name>
<located-at>2010-06-14T02:20:47-07:00</located-at>
<name>Station Approach, Tunbridge Wells, TN1 1</name>
<normal-name>S...
gistfile2.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* and once again, this time just as a script without the repl */
 
import dispatch._
import oauth._
import OAuth._
import scala.xml._
 
val fe = (:/("fireeagle.yahooapis.com") / "oauth").secure
val oauth = Consumer("[seekrit!]", "[also seekrit!]")
val h = new Http
val request_token = h(fe / "request_token" << Map("oauth_callback" -> "oob") <@ oauth as_token)
fe / "authorize" <<? request_token
(fe / "authorize" <<? request_token).to_uri
 
// At this point, the loyal user went off to the relevant URL and authenticated with the application.
// He returned with an OAuth verification token. What a nice user.
 
val access_token = h(fesecure / "access_token" << Map("oauth_verifier" -> "KXfQgG") <@ (oauth, request_token) as_token)
val feapi = (:/("fireeagle.yahooapis.com") / "api" / "0.1").secure
val location = XML.loadString(h(feapi / "user" <@ (oauth, access_token) as_str))
location \\ "location"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.