歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> play2.0實現新浪OAuth2.0

play2.0實現新浪OAuth2.0

日期:2017/3/1 10:10:23   编辑:Linux編程

最近有個項目要使用微博聯合登錄,看了下java版的 sdk, 太麻煩了,本來就不太懂java, 人都凌亂了,oauth2授權流程不是很復雜,就自己寫實現下吧,就當練手了。

庫代碼

package libs.oauth2

import play.api.libs.concurrent._

import play.api.libs.ws._
import play.core.parsers._
import play.api.libs.json._

case class OAuthKey(key:String, secret:String)
case class AccessToken(token:String)
case class AccessTokenError(error:String, errorCode:Int, request:String)
case class OAuth2(authorizationUrl: String, accessToken: String, redirectUri: String, key:OAuthKey){
def authorization(responseType:String, params:Map[String, String]) = {
val ism = if(authorizationUrl.indexOf("?") > 0) "&" else "?"
authorizationUrl+ism+"client_id="+key.key+"&response_type="+responseType+"&redirect_uri="+java.net.URLEncoder.encode(redirectUri, "utf-8")
}

def getAccessToken(code:String):Either[AccessToken, AccessTokenError] = {
val rs = WS.url(accessToken).withQueryString(
("client_id", key.key)
,("client_secret", key.secret)
,("grant_type", "authorization_code")
,("redirect_uri", redirectUri)
,("code", code)).post("content").value.get.body
var jsonRs = Json.parse(rs)
if(rs.indexOf("error") > 0){
Right(AccessTokenError((jsonRs \ "error").as[String],(jsonRs \ "error_code").as[Int], (jsonRs \ "request").as[String] ))
}else{
Left(AccessToken((jsonRs \ "access_token").as[String]))
}
}

}

附上用例

object login extends Controller {
val oauth2 = OAuth2(
"https://api.weibo.com/oauth2/authorize",
"https://api.weibo.com/oauth2/access_token",
"http://xx.com/login/sinare",
OAuthKey("key", "secret")
);
def sina = Action{
Redirect(oauth2.authorization("code",Map()))
}
def sinare = Action{ implicit request =>
request.queryString.getOrElse("code", "".toSeq).mkString match {
case code:String if !code.isEmpty => {
oauth2.getAccessToken(code) match{
case Left(t) => {
Ok("token:"+t.token)
}
case Right(error) => Ok("error"+error.error)
}
}
case _ => Ok("error")
}
}
}

Copyright © Linux教程網 All Rights Reserved