How to Log into Facebook Programmatically Using Java

How to log into Facebook programmatically using Java?

You should take a look at HTMLUnit, it'll be much simpler than using the above. The following page and code should guide you:

final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
final HtmlForm form = page1.getFormByName("login_form");

final HtmlSubmitInput button = form.getInputsByValue("Log in");
final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute("jon@jon.com");
final HtmlTextInput textField = form.getInputByName("pass");
textField.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();

http://htmlunit.sourceforge.net/gettingStarted.html

Using HTMLUnit to log into Facebook programmatically using Java

It looks like htmlunit doesn't like some of the javascript.

Try switching it off as it shouldn't be necessary for login:

webClient.setJavaScriptEnabled(false);

Get user accessToken with Java login for Facebook

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)

How to log out users using facebook connect in java with scribe library?

You have multiple options:

  1. Tie your Logout button to the Facebook Javascript SDK's
    FB.logout()
    function. This has a possibly unwanted side-effect of logging the
    user out of Facebook.com also (because your app might have been
    responsible for them logging into it in the first place)
  2. Use the /me/permissions Graph API endpoint with an HTTP DELETE and a user access token to revoke permissions for that user for your app. The downside here is that the user will have to re-authenticate again when they login to your app the next time (and see the Login Dialog again too).
  3. Store a session variable that tells you that someone is logged in. Delete that variable when they logout and store another session variable that indicates they have manually logged out. Then if that second variable exists, do not automatically re-authenticate them again until they click on the login button in your app.

How to log in with other facebook account?

You can login with different Facebook user id, after that:

  • Go to developers.facebook.com
  • choose Apps from Top
  • select wanted App from left side
  • select Edit App
  • disable sandbox mode

Can I create a facebook app automatically(from a java program) using any api from a java program or servlet?

In short, no you can't. You have to verify that you are a human when you create an app, and facebook intentionally do not provide a way to programmatically create apps.

how to access user feed/posts using android facebook sdk

This is documented on the User object of the Graph api. And, as of the Graph API v2.6, there is basically one main endpoint from which you get posts from a user.

  • /{user-id}/feed includes all the things that a user might see on their own profile feed; this includes, e.g., shared links, checkins, photos and status updates. This also includes posts made by friends on the user's profile.

The following endpoints return subsets of the above:

  • /{user-id}/posts returns the posts created by the user (on their own profile or the profile of a friend), and it may include any kind of content such as shared links, checkins, photos and status updates.
  • /{user-id}/tagged returns the posts created by friends and shared on the users's profile.

By default each returned post only includes the story field with a textual description of the post. But you can use the ?fields=... parameter to request as many Post fields as you want.

You'll need the user_posts permission for any of these to work.

The following endpoints are deprecated:

  • /{user-id}/statuses returns only status updates posted by the user on their own profile. [removed after Graph API v2.3]
  • /{user-id}/home returns a stream of all the posts created by the user and their friends, i.e. what you usually find on the “News Feed” of Facebook. [removed after Graph API v2.3]


Related Topics



Leave a reply



Submit