scala 如何在 Play Framework 2.0 中使用 OAuth 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10018604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use OAuth 2 in Play Framework 2.0
提问by William
So I am using scribe to connect to Facebook (OAuth 2). However I am having trouble getting the authorization token. On Play's websitethey say that
所以我使用 scribe 连接到 Facebook (OAuth 2)。但是我在获取授权令牌时遇到了问题。在Play 的网站上,他们说
"Version 2 is simple enough to be implemented easily without library or helpers,".
“第 2 版足够简单,无需库或助手即可轻松实现,”。
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.
但是,身份验证密钥包含一个“?” 在 url 中,所以我无法将其捕获为字符串。
Any help or advice would be appreciated!
任何帮助或建议将不胜感激!
回答by Vineet
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.
获取 OAuth2 很容易,但并非微不足道。有一个工作样本是有帮助的。我建议下载 Play1,并查找 Facebook 身份验证示例。然后将代码移植到 Play2。我做了上面的事情,发现这个过程非常有指导意义。您会意识到每个站点和 API 都有自己的怪癖/需求,因此从一个站点到另一个站点似乎可用的附加代码很少。
A more step-by-step answer is that there are several steps. First, you need to get an access_tokenand then you can use it. To get an access_tokenyou need to send the user to the sites authorization url, so far facebook this would be something like:
一个更分步的答案是有几个步骤。首先,您需要获得一个access_token,然后才能使用它。要获得一个access_token您需要将用户发送到网站授权网址,到目前为止 facebook 这将是这样的:
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:
一旦您的用户接受了授权,该站点将使用代码重定向用户,例如http://yourdomain.com/auth?code=XYZ_ABC. 然后,您需要从站点访问令牌 url 请求以获取访问令牌。对于 Facebook,这将类似于:
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_tokenin it.
来自上述 url 的响应将包含access_token在其中。
Now, you can start using the access token to request information.
现在,您可以开始使用访问令牌来请求信息。
回答by jleleu
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.
我不知道它是否有帮助,但我在 Scala 和 Java 中创建了一个 Play 2.x 客户端,它支持 OAuth/CAS/OpenID/HTTP 身份验证和用户配置文件检索:https: //github.com/leleuj/播放 pac4j。
For OAuth support, it's based on Scribe and supports Facebook, Twitter, Google, Yahoo, DropBox, Github, LinkedIn, Windows live, WordPress...
对于 OAuth 支持,它基于 Scribe 并支持 Facebook、Twitter、Google、Yahoo、DropBox、Github、LinkedIn、Windows live、WordPress...

![scala 私人 [this] 与私人](/res/img/loading.gif)