I do not think there is a way to get a user access token programmatically. Facebook requires the user's consent where the previously logged in user needs to press the "Okay" button in order to get a new access_token
.
However, there is a way to get an application access token
if this is what you intended.
public static void main(String[] args) {
try {
String myAppId = "XXX";
String myAppSecret = "XXX";
String redirectURI = "http://localhost:8080/";
String uri = "https://graph.facebook.com/oauth/access_token?client_id=" +
myAppId +
"&client_secret=" + myAppSecret +
"&grant_type=client_credentials" +
"&redirect_uri=" + redirectURI +
"&scope=user_about_me";
URL newURL = new URL(uri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = newURL.openStream();
int r;
while ((r = is.read()) != -1) {
baos.write(r);
}
String response = new String(baos.toByteArray());
is.close();
baos.close();
String TOKEN_INDEX = "accesss_token=";
String token = response.substring(TOKEN_INDEX.length()-1);
//API Call using the application access token
String graph = "https://graph.facebook.com/v2.2/" + myAppId +
"?access_token=" + token;
URL graphURL = new URL(graph);
HttpURLConnection myWebClient = (HttpURLConnection) graphURL.openConnection();
String responseMessage = myWebClient.getResponseMessage();
if (myWebClient.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println(myWebClient.getResponseCode() + " " + responseMessage);
} else {
System.out.println(responseMessage);
}
myWebClient.disconnect();
} catch (Exception e) {
System.err.println(e);
}
}
Note that because this request uses your app secret, it must never be made in client-side code or in an app binary that could be decompiled. It is important that your app secret is never shared with anyone. Therefore, this API call should only be made using server-side code.
Read here: https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens
BTW, we, at Stormpath support really simple social login integration (Google, Github, Facebook, LinkedIn)