How to authenticate users with facebook login in java [closed]

笑着哭i 提交于 2019-12-04 09:41:31

问题


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

but didn't get the solution


回答1:


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.

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();

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 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);

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");

See the class FacebookOAuthProcessor.




回答2:


I recommend to use 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



来源:https://stackoverflow.com/questions/23569036/how-to-authenticate-users-with-facebook-login-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!