[Scala]SecureSocialで取得したTwitterアクセストークンでタイムラインを取得するメモ[Play]


## はじめに

[Scala+Play+SecureSocialでTwitterにログイン認証してユーザ名とアバターを表示するメモ](http://qiita.com/JunSuzukiJapan@github/items/ff0412617b0e99a6166f)のコードに修正を加えて、Twitterのタイムラインを取得してみます。


## 修正箇所

タイムラインを取得するために、Twitter4jプラグインを使います。

### build.sbt

```build.sbt
import com.github.play2war.plugin._


name := "play221ss214SNSLogin"

version := "1.0-SNAPSHOT"

scalaVersion := "2.11.1"

libraryDependencies ++= Seq(
  jdbc,
  anorm,
  cache,
  "mysql" % "mysql-connector-java" % "5.1.18",
  "ws.securesocial" %% "securesocial" % "2.1.4",
   "org.twitter4j" % "twitter4j-core" % "4.0.1"
)     

play.Project.playScalaSettings

Play2WarPlugin.play2WarSettings

Play2WarKeys.servletVersion := "3.0"
```

>修正は１行だけですが、そんなに長くないので全部掲載。


### Application.scala

```scala:app/controllers/Application.scala
package controllers

import play.api._
import play.api.mvc._
import play.Play
import securesocial.core._


import twitter4j.auth.AccessToken
import twitter4j.TwitterFactory
import twitter4j.TwitterResponse
import twitter4j.ResponseList
import twitter4j.Status
import scala.collection.JavaConversions._


object Application extends securesocial.core.SecureSocial {
  val twitter = {
    val tw = TwitterFactory.getSingleton
    val consumer_key = Play.application().configuration().getString("securesocial.twitter.consumerKey")
    val consumer_secret = Play.application().configuration().getString("securesocial.twitter.consumerSecret")
    tw.setOAuthConsumer(consumer_key, consumer_secret)
    tw
  }


  def index = SecuredAction { implicit request =>
    // ここにくる時点でfullNameやアクセストークンは取得済み
    val name = request.user.fullName
    val provider = request.user.identityId.providerId
    
    val oAuth2Info:Option[OAuth2Info] = request.user.oAuth2Info
    var token = ""
    var tokenType: Option[String] = None
    var expiresIn: Option[Int] = None
    var refreshToken: Option[String] = None
    oAuth2Info match {
      case Some(info) => {
        token = info.accessToken
        tokenType = info.tokenType
        expiresIn = info.expiresIn
        refreshToken = info.refreshToken 
      }
      case None => println("oAuth2Info == None")
    }
    
    var timeline: ResponseList[twitter4j.Status] = null
    val oAuth1Info:Option[OAuth1Info] = request.user.oAuth1Info
    oAuth1Info match {
      case Some(info) => {
    	token = info.token
    	val secret = info.secret

   		val ac = new AccessToken(token, secret)
   		twitter.setOAuthAccessToken(ac)
   		timeline = twitter.getUserTimeline
      }
      case None => ()
    }
    
    Ok(views.html.index(name, provider, request.user.avatarUrl, timeline))
  }

  def page = UserAwareAction { implicit request =>
    val userName = request.user match {
      case Some(user) => user.fullName
      case _ => "guest"
    }
    Ok("Hello %s".format(userName))
  }

}
```

### index.scala.html


```html:app/views/index.scala.html
@(user: String, provider: String, avatarUrl: Option[String], timeline: twitter4j.ResponseList[twitter4j.Status])(implicit request: RequestHeader, lang: Lang)

@import helper._
@import securesocial.core.Registry
@import securesocial.core.AuthenticationMethod._
@import securesocial.core.providers.UsernamePasswordProvider.UsernamePassword


@main("Welcome") {
  <div class="page-header">
    <h1>Welcome!</h1>
  </div>

    Welcome @(user) at @(provider)!
    
    @avatarUrl match {
    	case Some(url) => { <image src="@url" /> }
    	case None => {}
    }
    <p>
	<ul>
	@for(tweet <- timeline){
		<li><p>@tweet.getText</p></li>
	}
	</ul>
    </P>
    
    <p>
    	<a href="/logout">Logout</a>
    </p>

}
```