How do I send a POST request to twitter API using Play Framework 2.0 (with Scala)? The API I'm trying to call work with both GET and POST, and I've successfully called it using GET with this code:
val followersURL = "http://api.twitter.com/1/users/lookup.json?user_id=" + listOfFollowers.mkString(",")
WS.url(followersURL)
.sign(OAuthCalculator(Twitter.KEY, tokens))
.get()
.map{ response =>
val screenName: Seq[String] = response.json match {
case res: JsArray => res.value.map{ value => (value \ "name").toString }
case _ => Seq("")
}
}
Then I tried to call the API using POST like this:
WS.url("http://api.twitter.com/1/users/lookup.json")
.sign(OAuthCalculator(Twitter.KEY, tokens))
.post(Map("user_id"->listOfFollowers))
.map { response =>
val screenName: Seq[String] = response.json match {
case res: JsArray => res.value.map{ value => (value \ "name").toString }
case _ => Seq("")
}
}
It didn't work and I get this exception:
[error] play - Waiting for a promise, but got an error: null
java.lang.NullPointerException: null
at java.io.Reader.<init>(Unknown Source) ~[na:1.7.0_01]
at java.io.InputStreamReader.<init>(Unknown Source) ~[na:1.7.0_01]
at oauth.signpost.OAuth.decodeForm(OAuth.java:157) ~[signpost-core.jar:na]
at oauth.signpost.AbstractOAuthConsumer.collectBodyParameters(AbstractOAuthConsumer.java:236) ~[signpost-core.jar:na]
at oauth.signpost.AbstractOAuthConsumer.sign(AbstractOAuthConsumer.java:96) ~[signpost-core.jar:na]
at play.api.libs.oauth.OAuthCalculator.sign(OAuth.scala:106) ~[play_2.9.1.jar:2.0.1]
Since it says that the exception occurs on the OAuthCalculator, I try to comment out the .sign
call, and it didn't throw any exception, but of course I didn't get the right result.
Am I doing something wrong? What am I doing wrong, and why? How could I fix the problem?
Thanks before.
I have found this to work:
WS.url("http://api.twitter.com/1/users/lookup.json?user_id="+listOfFollowers)
.sign(OAuthCalculator(Twitter.KEY, tokens))
.post("ignored")
.map { response =>
val screenName: Seq[String] = response.json match {
case res: JsArray => res.value.map{ value => (value \ "name").toString }
case _ => Seq("")
}
}
I've also made notes to revisit my code with every major upgrade of Play! to check if the above gets fixed, because this is obviously not right.
Once you use the Play Framework tools for getting your user's token and secret, you can then use the twitter4j ("org.twitter4j" % "twitter4j-core" % "3.0.3"
) library to do your posting like this.
import twitter4j.conf.ConfigurationBuilder
import twitter4j.{StatusUpdate, TwitterFactory}
val config = new ConfigurationBuilder()
.setOAuthConsumerKey(twitterKey)
.setOAuthConsumerSecret(twitterSecret)
.setOAuthAccessToken(token)
.setOAuthAccessTokenSecret(secret)
.build()
val twitter = new TwitterFactory(config).getInstance()
val status = new StatusUpdate(tweet)
status.media(photoName, stream)
val twitResp = twitter.updateStatus(status)
Annoying to have to use two libraries, and twitter4j isn't async so it's slightly less resource-efficient, but it lets you do real posts which are necessary sometimes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With