如何在java中使用facebook登录对用户进行身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23569036/
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 authenticate users with facebook login in java
提问by TechChain
Hi i am working on a web project using java i want to user of website to log in using their Facebook account.I tried Facebook Developer official
嗨,我正在使用 java 开发一个 web 项目,我想让网站用户使用他们的 Facebook 帐户登录。我尝试了Facebook 开发人员官方
but didn't get the solution
但没有得到解决方案
回答by Leos Literak
I can recommend you OAuth library Scribe, or its improved fork Subscribe. If you are interested in usage samples, take a look at my project OAuh JEE Login.
我可以向您推荐 OAuth 库Scribe或其改进的分支Subscribe。如果您对使用示例感兴趣,请查看我的项目Oauh JEE Login。
First you need to get OauthService instance:
首先你需要获取 OauthService 实例:
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey("key").apiSecret("secret")
.scope("email").callback("https://hostname/endpoint/")
.build();
Then you need to redirect user to facebook page where he will grant access to your application / log in:
然后您需要将用户重定向到 facebook 页面,他将在那里授予对您的应用程序/登录的访问权限:
String redirectUrl = service.getAuthorizationUrl(NULL_TOKEN);
response.sendRedirect(redirectUrl);
Facebook will then call your callback uri and you need to grab parameter code
and get access token:
Facebook 然后将调用您的回调 uri,您需要获取参数code
并获取访问令牌:
String verifierValue = request.getParameter("code");
if (verifierValue == null) {
log.warn("Callback did not receive code parameter!");
return false;
}
Verifier verifier = new Verifier(verifierValue);
Token accessToken = service.getAccessToken(NULL_TOKEN, verifier);
Finally it is time to use this token to ask Facebook for user details:
最后是时候使用此令牌向 Facebook 询问用户详细信息:
OAuthRequest resourceRequest = new OAuthRequest(Verb.GET, RESOURCE_URL);
service.signRequest(accessToken, resourceRequest);
Response resourceResponse = resourceRequest.send();
JsonObject jsonObject = JsonObject.readFrom(resourceResponse.getBody());
JsonValue value = jsonObject.get("username");
See the class FacebookOAuthProcessor.
回答by Phat H. VU
I recommend to use Scribe.
我建议使用 Scribe。
Check out an example of how to using Facebook with scribe here : https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/FacebookExample.java
在此处查看如何将 Facebook 与 scribe 一起使用的示例:https: //github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/FacebookExample.java