Skip to content

ymnk/OAuS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OAuS is an implementation of OAuth[1] client in Scala.

[1] http://www.rfc-editor.org/rfc/rfc5849.txt

How To Use
==========
  /* Using java.net.HttpURLConnection */

  val url = new URL("http://example.com/foo")
  val urlConn = new URL(url).openConnection.asInstanceOf[HttpURLConnection]
 
  import com.jcraft.oaus.{OAuthClient, ClientCredential, TokenCredential}
  val clientCredential = ClientCredential(CONSUMER_KEY, CONSUMER_SECRET)
  val tokenCredential = TokenCredential(ACCESS_TOKEN, TOKEN_SECRET)
  val oac = new OAuthClient(clientCredential)
  oac.signGetRequest(url, tokenCredential) {
    (k, v) => urlConn.setRequestProperty(k, v)
  }

  urlConn.connect
  urlConn.getReponseCode

  /*****/

  val url = new URL("http://example.com/foo")
  val urlConn = new URL(url).openConnection.asInstanceOf[HttpURLConnection]
  urlConn.setRequestMethod("POST")     
  urlConn.setDoOutput(true);

  val data = "foo=1&bar=2"

  import com.jcraft.oaus.{OAuthClient, ClientCredential, TokenCredential}
  val clientCredential = ClientCredential(CONSUMER_KEY, CONSUMER_SECRET)
  val tokenCredential = TokenCredential(ACCESS_TOKEN, TOKEN_SECRET)
  val oac = new OAuthClient(clientCredential)
  oac.signPostRequest(url, data, tokenCredential) {
    (k, v) => urlConn.setRequestProperty(k, v)
  }

  val writer = new java.io.PrintWriter(urlConn.getOutputStream)
  writer.print(data)
  writer.close()

  urlConn.connect
  urlConn.getReponseCode

  /*****/

  /* using commons-httpclient */

  import com.jcraft.oaus._
  import org.apache.commons.httpclient._
  import org.apache.commons.httpclient.methods._

  val url = "http://api.twitter.com/1/statuses/update.xml"
  val client = new HttpClient
  val method = new PostMethod(url)

  val tweet = "Hello "+(new java.util.Date)

  method.addParameter("status", tweet);

  import com.jcraft.oaus.{OAuthClient, ClientCredential, TokenCredential}
  val clientCredential = ClientCredential(CONSUMER_KEY, CONSUMER_SECRET)
  val tokenCredential = TokenCredential(ACCESS_TOKEN, TOKEN_SECRET)
  val oac = new OAuthClient(clientCredential)
  oac.signPostRequest(url, 
                      "status="+tweet, 
                      tokenCredential) {
    method.setRequestHeader(_, _)
  }

  println(clinet.executeMethod(method))
  println(xml.XML.load(method.getResponseBodyAsStream))

  method.releaseConnection


Examples
========
The 'examples' directory includes following examples,
  * Tweet.scala
    This is a demonstration to tweet with OAuth authentication.
    You need to specify CONSUMER_KEY, CONSUMER_SECRET, etc., to try it.
  * GetTokenCredential.scala    
    This is a demonstration to show how to get token credentials for Twitter.
    You need to specify CONSUMER_KEY, CONSUMER_SECRET to try it.
  * GetTokenCredentialWithSHASHA1.scala
    This is a demonstration to show how to get token credentials for Google with
    SHA-SHA1 signature method.
    You need to specify CONSUMER_KEY, CONSUMER_SECRET and give the private-key in
    PKCS#8 in DER format as an argument to try it.


Terminology
===========
The terminology used in RFC5849 are different from the terminology in
the previous spec(http://oauth.net/core/1.0a/).  In developing OAuS,
we have adopted the terminology from RFC5849, but all of the documents on
the web have used the previous one, unfortunately. 
If you are familiar with the older terminology, following mapping may be 
useful in using OAuS,

  http://oauth.net/core/1.0a/ |   RFC5849
  Consumer:                   |   client
  Service Provider:           |   server
  User:                       |   resource owner
  Consumer Key and Secret:    |   client credentials
  Request Token and Secret:   |   temporary credentials
  Access Token and Secret:    |   token credentials


About

OAuth clinet in Scala

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages