【问题标题】:scala play twitter api oauth authentication doesnt workscala play twitter api oauth 身份验证不起作用
【发布时间】:2015-09-02 22:38:43
【问题描述】:

使用 play 2.4 通过 oauth 访问 twitter api。 我在 twitter 上创建了一个应用程序,并创建了带有秘密的密钥和令牌。 在 twitter 上为我的应用程序创建了一个 URL,因为它实际上并不存在。 并在回调 url 上使用了相同的组成 url。在 localhost 上运行应用程序。使用来自 twitter 的 curl 生成的命令来模拟身份验证按预期工作,因此我的密钥和令牌是有效的。

当我运行我的代码时,我得到了这个响应:

错误 401 未经授权

这是主要播放代码:

import play.api._
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.Play.current
import play.api.libs.oauth.{ConsumerKey,RequestToken}
import scala.concurrent.Future
import play.api.libs.ws._
import play.api.libs.oauth.OAuthCalculator
import play.api.libs.iteratee._

class Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

  val loggingIteratee = Iteratee.foreach[Array[Byte]] { array => Logger.info(array.map(_.toChar).mkString) }

  def tweets = Action.async {
    credentials.map {
      case (consumerKey, requestToken) =>
        println(consumerKey.key.toString())
        println(consumerKey.secret.toString())
        println(requestToken.token.toString())
        println(requestToken.secret.toString())
        WS
          .url("https://stream.twitter.com/1.1/statuses/sample.json")
          .sign(OAuthCalculator(consumerKey, requestToken))
          .withQueryString("track" -> "reactive")
          .get()
          .map { response =>
            Ok(response.body)
          }
    }getOrElse{
      Future{
        InternalServerError("twitter credentials missing")
      }
    }
  }

  def credentials :Option[(ConsumerKey,RequestToken)] = for {
      apiKey <- Play.configuration.getString("twitter.apiKey")
      apiSecret <- Play.configuration.getString("twitter.apiSecret")
      token <- Play.configuration.getString("twitter.token")
      tokenSecret <- Play.configuration.getString("twitter.tokenSecret")
  }yield (ConsumerKey(apiKey,apiSecret),RequestToken(token,tokenSecret) )

}

【问题讨论】:

  • Play 2.4 oauth 已损坏。
  • @bwawok 它有错误吗?就像这是一个已知问题?
  • 我想是的,在github.com/playframework/playframework/pull/4826 进行故障排除。到目前为止,使用 play 2.4 升级的异步 http 似乎有问题。我敢打赌你的代码在 play 2.3 中有效。
  • 我认为 Play 2.4 oauth 没有损坏,因为我刚刚让它为我工作。见我下面的帖子。无论如何感谢您的意见..

标签: scala twitter oauth playframework


【解决方案1】:

我认为问题在于您使用了虚假的 Twitter API。正如提到的here,您必须使用 Twitter API 1.1,因此您的 Web 服务的 URL 应该是:.url("https://stream.twitter.com/1.1/statuses/sample.json")

【讨论】:

  • 你使用的 cURL 是来自 twitter 的 twurl 还是普通的 cURL?
  • 是的,我使用 CURL 检查了生成的令牌和密钥,它运行良好。
【解决方案2】:

我的问题一定出在我在 twitter 上创建的 twitter 应用程序上。删除并重新创建了应用程序,它工作正常。我正在使用 play 2.4 。我能想到的唯一区别是,当我第一次在 twitter 上创建应用程序时,我提供了一个虚构的 url,它实际上并不存在并且它没有工作。然后我删除了该应用程序并创建了一个新应用程序,但提供了我的 twitter 帐户 url 作为 web url 并且它有效。

【讨论】:

    猜你喜欢
    • 2011-10-12
    • 2011-06-18
    • 2011-06-10
    • 2011-11-16
    • 2013-04-15
    • 2015-03-13
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多