如何在java中使用facebook登录验证用户 [英] How to authenticate users with facebook login in java
问题描述
您好我正在使用java进行网络项目我希望网站用户使用他们的Facebook帐户登录。我试过 Facebook开发官员
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
但没有得到解决方案
推荐答案
我可以向您推荐OAuth库 Scribe ,或其改进的fork Subscribe 。如果您对使用示例感兴趣,请查看我的项目 OAuh JEE登录。
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.
首先你需要获得OauthService实例:
First you need to get OauthService instance:
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey("key").apiSecret("secret")
.scope("email").callback("https://hostname/endpoint/")
.build();
然后,您需要将用户重定向到Facebook页面,在那里他将授予对您的应用程序/登录的访问权限:
Then you need to redirect user to facebook page where he will grant access to your application / log in:
String redirectUrl = service.getAuthorizationUrl(NULL_TOKEN);
response.sendRedirect(redirectUrl);
Facebook将调用你的回调uri,你需要获取参数代码
并获取访问令牌:
Facebook will then call your callback uri and you need to grab parameter code
and get access token:
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);
最后是时候使用此令牌向Facebook询问用户详情:
Finally it is time to use this token to ask Facebook for user details:
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");
参见班级FacebookOAuthProcessor 。
这篇关于如何在java中使用facebook登录验证用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!