如何在Scala Play框架中进行Twitter反向身份验证? [英] How can I do a Twitter Reverse Auth In Scala Play Framework?

查看:125
本文介绍了如何在Scala Play框架中进行Twitter反向身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在(在scala中)编写一个播放应用程序,并试图执行twitter在此处概述的反向身份验证步骤:

I'm writing a play application (in scala) and I'm trying to perform the reverse-auth step that is outlined by twitter here: https://dev.twitter.com/docs/ios/using-reverse-auth

该步骤听起来像是普通的RetrieveRequestToken,用于 https://api.twitter.com/oauth/request_token 以及设置x_auth_mode = reverse_auth

The step sounds like an ordinary RetrieveRequestToken to https://api.twitter.com/oauth/request_token with the additional parameter of setting the x_auth_mode=reverse_auth

播放框架利用了Sign-Post( https://code.google.com /p/oauth-signpost/),我想知道如何利用WS库和Sign-Post实现这一目标.

THe play framework makes use of Sign-Post (https://code.google.com/p/oauth-signpost/) and I was wondering how I can leverage the WS library and Sign-Post to make this happen.

这是我想出的:

val KEY = ConsumerKey(Play.configuration.getString("twitter.consumer.key").getOrElse("XXXX"),               Play.configuration.getString("twitter.consumer.secret").getOrElse("XXXX"))

  def reverse = Action.async {
    val oauth_calc = OAuthCalculator(KEY,  RequestToken("","") )
    oauth_calc.setSigningStrategy(new oauth.signpost.signature.AuthorizationHeaderSigningStrategy )
    var reverse_request : Future[Response] = WS.url("https://api.twitter.com/oauth/request_token").sign(oauth_calc)
         .post(Map("x_auth_mode" -> Seq("reverse_auth")))     
      reverse_request.map{
        response => {
          Logger.debug(response.body)
          Ok("Here")
        }
      }
  }

但是我收到无法验证oauth签名和令牌"

I'm getting however "Failed to validate oauth signature and token"

推荐答案

所以我在这里找到了部分答案: https://stackoverflow.com/a/20635782/143733 我不得不使用apache默认的HttpClient,因为在Play WS类或Sign-Post中似乎没有办法.

So I found part of the answer here: https://stackoverflow.com/a/20635782/143733 I had to use the apache default HttpClient as there seemed no way to do it in Play WS class or Sign-Post.

这是我想出的最后一个异步实现:

Here is the final async implementation I came up with:

object Twitter extends Controller {

  val KEY = ConsumerKey(Play.configuration.getString("twitter.consumer.key").getOrElse("XXXX"), 
                        Play.configuration.getString("twitter.consumer.secret").getOrElse("XXXX"))

  val TWITTER_SERVICE_INFO = ServiceInfo(
    "https://api.twitter.com/oauth/request_token",
    "https://api.twitter.com/oauth/access_token",
    "https://api.twitter.com/oauth/authorize", KEY)

  val TWITTER = OAuth(TWITTER_SERVICE_INFO,false)

  /***
   * The following is the reverse oauth step outlined by twitter 
   */
  def reverse = Action.async {

    val client = new DefaultHttpClient();
    val params = new java.util.ArrayList[BasicNameValuePair](1)
    params.add(new BasicNameValuePair("x_auth_mode", "reverse_auth"))
    val consumer = new CommonsHttpOAuthConsumer(KEY.key, KEY.secret)
    val post = new HttpPost(TWITTER_SERVICE_INFO.requestTokenURL)
    post.addHeader("Content-Type", "application/x-www-form-urlencoded")
    post.setEntity(new UrlEncodedFormEntity(params))
    consumer.sign(post)

    val futureBodyString: Future[String] = future {
      val response = client.execute(post)
      EntityUtils.toString(response.getEntity())
    }

    futureBodyString.map {
      body => {
        Ok(body)
      }
    }

  }


}

这篇关于如何在Scala Play框架中进行Twitter反向身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆