OAuth在用框架2.2.2
我一直在寻找一些关于如何在Play框架(版本2.2.2)中执行OAuth的文档,但我找不到任何东西。我在一个地方读到它已经被弃用了,但我也没有找到任何关于它的东西。有人知道吗?我想连接到Twitter API并在我的应用程序中请求一些数据。您可以在这些开源项目中找到OAuth with Play Framework的示例:OAuth在用框架2.2.2,oauth,playframework,Oauth,Playframework,我一直在寻找一些关于如何在Play框架(版本2.2.2)中执行OAuth的文档,但我找不到任何东西。我在一个地方读到它已经被弃用了,但我也没有找到任何关于它的东西。有人知道吗?我想连接到Twitter API并在我的应用程序中请求一些数据。您可以在这些开源项目中找到OAuth with Play Framework的示例: 它是受支持的,实际上非常简单 下面是一个OAuth授权示例,直接来自: 请注意,以上内容适用于OAuth 1.0。OAuth2在没有专用库的情况下很容易实现,这就是为
请注意,以上内容适用于OAuth 1.0。OAuth2在没有专用库的情况下很容易实现,这就是为什么剧组的人忽略了它。作者征求了意见,你告诉他它“很容易实现”~这不是很有建设性。
object Twitter extends Controller {
val KEY = ConsumerKey("xxxxx", "xxxxx")
val TWITTER = OAuth(ServiceInfo(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize", KEY),
false)
def authenticate = Action { request =>
request.queryString.get("oauth_verifier").flatMap(_.headOption).map { verifier =>
val tokenPair = sessionTokenPair(request).get
// We got the verifier; now get the access token, store it and back to index
TWITTER.retrieveAccessToken(tokenPair, verifier) match {
case Right(t) => {
// We received the authorized tokens in the OAuth object - store it before we proceed
Redirect(routes.Application.index).withSession("token" -> t.token, "secret" -> t.secret)
}
case Left(e) => throw e
}
}.getOrElse(
TWITTER.retrieveRequestToken("http://localhost:9000/auth") match {
case Right(t) => {
// We received the unauthorized tokens in the OAuth object - store it before we proceed
Redirect(TWITTER.redirectUrl(t.token)).withSession("token" -> t.token, "secret" -> t.secret)
}
case Left(e) => throw e
})
}
def sessionTokenPair(implicit request: RequestHeader): Option[RequestToken] = {
for {
token <- request.session.get("token")
secret <- request.session.get("secret")
} yield {
RequestToken(token, secret)
}
}
}
WS.url(s"https://api.twitter.com/1.1/account/verify_credentials.json")
.sign(OAuthCalculator(Key, RequestToken(token, tokenSecret)))
.get