Skip to content

Instantly share code, notes, and snippets.

@buka
Created December 30, 2010 05:28
Show Gist options
  • Save buka/759501 to your computer and use it in GitHub Desktop.
Save buka/759501 to your computer and use it in GitHub Desktop.
Connecting Facebook OAuth with Akka REST...
/**
* Garrick Evans
* 29 Dec 2010
*/
import akka.http._
import akka.actor._
import net.smartam.leeloo.client._
import net.smartam.leeloo.client.request.OAuthClientRequest
import net.smartam.leeloo.client.response. {OAuthAuthzResponse, GitHubTokenResponse}
import net.smartam.leeloo.common.message.types.GrantType
class Service extends Actor with Endpoint
{
def hook(uri:String) = uri contains ".html"
def provide(uri:String) = Actor.actorOf[PageActor].start
override def preStart =
{
ActorRegistry.actorsFor(classOf[RootEndpoint]).head ! Endpoint.Attach(hook, provide)
}
def receive = handleHttpRequest
}
object Service
{
val server = "http://10.10.1.1:9998" // <--- Your server addr here
}
class PageActor extends Actor
{
def receive =
{
case get:Get => {
get.request.getRequestURI match {
case page if page == "/index.html" => {
// steps from the leeloo wiki - https://bitbucket.org/smartproject/oauth-2.0/wiki/Client
val oauthreq = OAuthClientRequest
.authorizationLocation("https://graph.facebook.com/oauth/authorize")
.setClientId("0101") // <--- Your application ID here
.setRedirectURI(Service.server+"/auth.html")
.buildQueryMessage
get.response.sendRedirect(oauthreq.getLocationUri)
}
case page if page startsWith "/auth.html" => {
val code = OAuthAuthzResponse.oauthCodeAuthzResponse(get.request).getCode
log.slf4j.info("Got auth code: {}", code)
val oauthreq = OAuthClientRequest
.tokenLocation("https://graph.facebook.com/oauth/access_token")
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId("0101") // <-- Your application ID here
.setClientSecret("deadbeef") // <-- Your applciation secret here
.setRedirectURI(Service.server+"/auth.html") // <-- Must be the same URI as previous
.setCode(code)
.buildBodyMessage
//create OAuth client that uses custom http client under the hood
val client = new OAuthClient(new URLConnectionClient)
//Facebook is not fully compatible with OAuth 2.0 draft 10, access token response is
//application/x-www-form-urlencoded, not json encoded so we use dedicated response class for that
//Custom response classes are an easy way to deal with oauth providers that introduce modifications to
//OAuth 2.0 specification
val oatuhresp = client.accessToken(oauthreq, classOf[GitHubTokenResponse]);
val token = oatuhresp getAccessToken
val expiry = oatuhresp getExpiresIn
log.slf4j.info("Got token {} expiry {}", token, expiry)
get OK "ok"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment