There is the Scribe which serves to treat such authentication.
In the README project supports Facebook and other networks using the same type of authentication protocol: Oauth. Perhaps you will wonder how it works, nothing is automatic as when you authorize access to your information through a web application:
connect with Facebook ... authorize ... ready.
In the case of a desktop application the flow for authentication is manual. The API will return an authorization URL, the user will have to copy this URL and open it in an internet browser - this you can do in your application, open the default browser right on the authorization page.
After allowing access, the user will receive an authorization code that must be returned to Scribe to continue the authentication flow, ie copy the code there in the browser and paste into some input field in your application, maybe this is the boring part.
A example to connect with Facebook taken 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] dealing with the use of this library.
See this question in the Soen: http://stackoverflow.com/questions/4883549/how-to-connect-facebook-with-java-desktop-application
– Fellipe Soares