Submit a POST request to Twitter API with Play Framework 2.0

How to send a POST request to twitter API using Play Framework 2.0 (using Scala)? The API I'm trying to call with both GET and POST, and I successfully called it with GET using 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 as follows:

 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("") } } 

This did not 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 he says the exception is happening on the OAuthCalculator, I am trying to comment on the .sign call, and it did not raise any exceptions, but of course I did not get the result I needed.

Am I doing something wrong? What am I doing wrong and why? How can I fix the problem?

Thanks before.

+6
source share
2 answers

I 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 also took notes to review my code with every major update to the Play! to check if this is fixed above because it is clearly not.

+4
source

Once you use the Play Framework tools to get the user's token and privacy, you can use the twitter4j library ( "org.twitter4j" % "twitter4j-core" % "3.0.3" ) to make your publication as follows.

 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) 

The need to use two libraries is annoying, and twitter4j is not asynchronous, so it is a little less resource efficient, but it sometimes allows you to make real messages, which are sometimes necessary.

+1
source

Source: https://habr.com/ru/post/918810/


All Articles