There is Scribe that serves to handle this type of authentication.
No README of the project includes support for Facebook and other networks that use the same type of authentication protocol: OAuth . Maybe you'll wonder how it works, nothing is automatic like when you authorize access to your information through a web application:
...
In the case of a desktop application the stream for authentication is manual. The API will return an authorization URL, the user will have to copy that URL and open it in a web browser - this you can do in your application, open the default browser directly on the authorization page.
After allowing access, the user will receive an authorization code that should be returned to Scribe to continue the authentication stream, ie copy the code there in the browser and paste in some input field in your application, maybe this is the boring part.
A example for connect with Facebook removed from the project repository:
import java.util.*;
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
public class FacebookExample {
private static final String PROTECTED_RESOURCE_URL = "https://graph.facebook.com/me";
private static final Token EMPTY_TOKEN = null;
public static void main(String[] args) {
// Altere com sua API Key e API secret:
String apiKey = "your_app_id";
String apiSecret = "your_api_secret";
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.callback("http://www.example.com/oauth_callback/")
.build();
Scanner in = new Scanner(System.in);
System.out.println("=== Fluxo de autenticação OAuth pelo Facebook ===");
System.out.println();
System.out.println("Buscando a URL de autorização");
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
System.out.println("URL de autorização obtida.");
System.out.println("Agora vá e autorize o Scribe nesse link: ");
System.out.println(authorizationUrl);
System.out.println("E cole o código de autorização aqui");
System.out.print(">>");
Verifier verifier = new Verifier(in.nextLine());
System.out.println();
System.out.println("Negociando o token de requisição para obter o token de acesso...");
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
System.out.println("Token de acesso obtido!");
System.out.println("(Se estiver curioso, o Token de acesso se parece com isso: " + accessToken + " )");
System.out.println();
System.out.println("Agora nós vamos acessar um recurso protegido ...");
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println("Feito! Vamos ver o que encontramos...");
System.out.println();
System.out.println(response.getCode());
System.out.println(response.getBody());
System.out.println();
System.out.println("É isso! Vá e crie algo incrível com Scribe! :)");
}
}
There are some questions in StackOverflow [en] which address the use of this library.