How to use OAuth 2 in Play Framework 2.0

Go To StackoverFlow.com

8

So I am using scribe to connect to Facebook (OAuth 2). However I am having trouble getting the authorization token. On Play's website they say that

"Version 2 is simple enough to be implemented easily without library or helpers,".

However, I'm not quite sure how to do this!

I tried changing my routes file that would send the key to a built method.

GET    /slivr_auth/*name        controllers.Application.getKey(name)

However, the auth key contains a '?' in the url, so I can't capture it as a string.

Any help or advice would be appreciated!

2012-04-04 20:15
by William
Did you solve this - 030 2017-02-13 09:07
In case that helps, the author of the "Play Framework Essentials" book, Julien Richard-Foy, has included an example in the github repo of the book. I haven't looked at it much as I opted to use Silhouette instead, but it may offer some good insight - acros 2017-12-03 16:27


4

To answer your specific question, you can get request (query) parameters by calling:

Controller.request().queryString()

Getting OAuth2 is easy but not trivial. It helps to have a working sample. I would recommend downloading Play1, and looking up the sample for Facebook Authentication. And then porting the code over to Play2. I did the above and found the process very instructive. You will realize that each site and API has quirks/needs, so there is very little additional code that seems usable form one site to another.

A more step-by-step answer is that there are several steps. First, you need to get an access_token and then you can use it. To get an access_token you need to send the user to the sites authorization url, so far facebook this would be something like:

https://graph.facebook.com/oauth/authorize/?client_id=idFromFacebook&redirect_uri=http://yourdomain.com/auth

Once your user has accepted the authorization, the site will redirect the user with a code, something like http://yourdomain.com/auth?code=XYZ_ABC. You would then need to request from the sites access token url to get the access token. For Facebook this would be something like:

https://graph.facebook.com/oauth/access_token?client_id=idFromFacebook&client_secret=secredFromFacebook&code=XYZ_ABC&redirect_uri=...

The response from the above url would have the access_token in it.

Now, you can start using the access token to request information.

2012-04-13 00:45
by Vineet


1

I don't know if it might help, but I've created a Play 2.x client in Scala and Java which supports OAuth/CAS/OpenID/HTTP authentication and user profile retrieval : https://github.com/leleuj/play-pac4j.

For OAuth support, it's based on Scribe and supports Facebook, Twitter, Google, Yahoo, DropBox, Github, LinkedIn, Windows live, WordPress...

2013-02-21 09:41
by jleleu


0

I have checked multiple examples, but this one works out of the box.

  1. clone the repo
  2. navigate to samples/java/play-authenticate-simple-oauth
  3. create Google credentials and add the clientId and clientSecret to conf/play-authenticate/mine.conf
  4. run sbt run
  5. navigate to localhost:9000
  6. login
2017-02-13 11:16
by 030
Ads