我一直在尋找一些有關如何在Play框架(版本2.2.2)中執行OAuth的文檔,但我找不到任何東西。我在一個地方看過它已被棄用,但我還沒有找到任何關於這個的地方。有人知道嗎?我想連接到Twitter API並在我的應用程序中提出一些數據請求。Play Framework中的OAuth 2.2.2
2
A
回答
1
你可以找到的OAuth的例子對這些開源項目中發揮框架:
-1
它的支持,實際上非常直截了當。
下面直接是一個OAuth授權例如從Play Docs:
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
請注意,上面是對的OAuth 1.0。如果沒有專門的庫,那麼OAuth2就很容易實現,這就是爲什麼Play人員將其遺留下來的原因。
相關問題
- 1. Ebean與Play Framework的交易傳播2.2.2
- 2. Play Framework和Office 365 OAuth
- 3. 如何在Play Framework 2.0中使用OAuth 2
- 4. Play Framework SBT import play.api.libs.streams
- 5. Play 2.2.2/com.typesafe.play#play_2.10; 2.2.2:在Heroku上找不到
- 6. Chrome中的Play Framework 2.1 websockets
- 7. Angularjs + OAuth + Play 2.0
- 8. Zend Framework 2中的OAuth
- 9. 使用Play Framework無法連接到遠程Heroku Postgres數據庫2.2.2
- 10. Play Framework Regex CORS
- 11. Play Framework和Threadpools
- 12. QueryDSL和Play Framework
- 13. Play Framework CRUD
- 14. Play Framework和DB2
- 15. Play Framework和scala.tools.nsc
- 16. Play Framework 2 HornetQ
- 17. Play Framework 2.4 Ebean
- 18. Play Framework + tomcat
- 19. akka with play framework
- 20. Play Framework + SecureSocial:java.lang.NoSuchMethodError:securesocial.controllers.ReverseLoginPage.authenticate
- 21. Play Framework - save()
- 22. DTO in Play Framework
- 23. Play Framework 2 slugify
- 24. play-framework [2.0] HTTPS
- 25. Play Framework和Python
- 26. 如何修改play play framework 2.4.x中的java play i18n lang?
- 27. NullPointerException:null在Play Framework 2.3.1中
- 28. 在Play Framework中使用SchemaExport
- 29. Json在Play Framework中閱讀
- 30. 連接到數據庫的Play Framework
作者提出了建議 - 並告訴他「很容易實現」〜這不是很有建設性。 – bharal