How to use OAuth 2 in Play Framework 2.0

So, I use a scribe to connect to Facebook (OAuth 2). However, I am having trouble getting an authorization token. On the Play website say that

"Version 2 is simple enough so that it can be easily implemented without a library or helpers."

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

I tried to modify the routes file that would send the key to the inline method.

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

However, the auth key contains '?' in the url so i cannot commit it as a string.

Any help or advice would be appreciated!

+8
java scala playframework
source share
3 answers

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

 Controller.request().queryString() 

Getting OAuth2 is easy, but not trivial. This helps to have a working pattern. I would recommend downloading Play1 and see a sample Facebook authentication. And then port the code to Play2. I did this and found the process very instructive. You will realize that every site and API has quirks / needs, so there is very little additional code that seems useful for one site for another.

A more phased answer is that there are several steps. First, you need to get access_token , and then you can use it. To get access_token , you need to send the user to the site authorization URL, so far it will be something like this:

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

As soon as your user accepts the authorization, the site will redirect the user with a code, something like http://yourdomain.com/auth?code=XYZ_ABC . Then you will need to request access to the url token from the sites to get the access token. For Facebook, it will 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 will contain access_token .

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

+4
source share

I don’t know if this can help, but I created a Play 2.x client in Scala and Java that supports OAuth / CAS / OpenID / HTTP authentication and user profile search: https://github.com/leleuj/play-pac4j .

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

+1
source share

I checked a few examples, but this one works out of the box.

  • clone repo
  • go to samples/java/play-authenticate-simple-oauth
  • create google credentials and add clientId and clientSecret to conf/play-authenticate/mine.conf
  • run sbt run
  • go to localhost: 9000
  • To come in
0
source share

All Articles