Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A minimalist Twitter client which highlights new tweets

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 readme
Octocat-spinner-32 sbt
Octocat-spinner-32 src
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
Octocat-spinner-32 build.sbt
README.md

Consuming the Twitter API in Scala with Scribe

9 May 2011

Nuevos is a minimalist Twitter client which highlights unread tweets. Its purpose is to provide at a glance an easy way to get updates of one's own timeline.

Nuevos remembers which tweets you have most recently seen regardless of which Web browser you use, so that state is kept up to date irrespective of the computer (or phone) you use.

Nuevos is currently quite beta, but seems to be stable enough for a few users.

Scribe is an OAuth library for Java which comes with built-in support for many APIs.

Scribe is configured by instantiating an OAuthService:

def service: OAuthService = new ServiceBuilder()
  .provider(classOf[TwitterApi])
  .apiKey(Secret.consumerKey)
  .apiSecret(Secret.consumerSecret)
  .callback(Secret.authCallback)
  .build

The consumer key, consumer secret, and auth callback are all assigned by Twitter to an application through the Twitter developers site.

Twitter API calls must be signed via OAuth, which is a multi-step process. First, a user needs to authenticate and grant access with Twitter:

def requestToken(): Token = service.getRequestToken
def getAuthUrl(requestToken: Token): String = service.getAuthorizationUrl(requestToken)

Once access has been granted by the user, his access token is needed to sign future API calls:

def getAccessToken(requestToken: Token, verifierCode: String): Token = {
  val verifier: Verifier = new Verifier(verifierCode);
  service.getAccessToken(requestToken, verifier);
}

Now the application can use this access token to make signed API calls:

def get(url: String, accessToken: Token): String = {
  request(Verb.GET, url, accessToken)
}

def request(verb: Verb, url: String, accessToken: Token): String = {
  val request: OAuthRequest = new OAuthRequest(verb, url)
  service.signRequest(accessToken, request)
  val response: Response = request.send
  if (response.getCode == 401) throw new UnauthorizedException
  response.getBody
}

def homeTimeline(accessToken: Token): NodeSeq = {
  val url = "http://api.twitter.com/1/statuses/home_timeline.xml"
  val response: String = get(url, accessToken)
  val statuses = XML.loadString(response)
  statuses \ "status"
}

That's about it! Calling homeTimeline above will return a sequence of nodes, each of which contains a tweet (or "status" in Twitter API parlance).

I built this code into a minimalistic Twitter client designed to quickly show me what new tweets have been added to my home timeline. The result is a simple Scalatra application which looks a little like the following. Note that for the screenshots below, I changed my API call to user_timeline so that only my own tweets would show.

Initial authentication and access grant

Upon first access, all tweets are unread

Clicking on a tweet reveals extra information about it

Subsequent access shows that all tweets have been read

Something went wrong with that request. Please try again.