0

I am writing a java application in which I need to access my chat history (chat messages between me and another Facebook friend). I have looked at this link, but it seems outdated since I have noticed that Facebook changed his messenger API significantly. I was wondering if it is still possible to access my message history via java.

p.s. I found a good Facebook Graph API called restfb. But I was not able to find such an API for chat messages.

Mohammadreza
  • 450
  • 2
  • 4
  • 14
  • possible duplicate of [Replacement for Facebook Chat API in v2.0?](http://stackoverflow.com/questions/23988889/replacement-for-facebook-chat-api-in-v2-0) – kevcodez Jul 05 '15 at 18:52
  • Thanks. I looked at the thread you mentioned but still it is not about accessing chat messages "in java". As far as I could understand the workaround is using graph API as Dathan mentioned but still I am not sure if I can use it in java. – Mohammadreza Jul 05 '15 at 19:28

1 Answers1

3

You can use the inbox resource of the Graph API: https://developers.facebook.com/docs/graph-api/reference/v2.3/user/inbox

Edit:

In order to use this from Java, you'll need to first follow the login instructions at https://developers.facebook.com/docs/facebook-login/v2.3 . That's a large enough operation that I'm going to assume that you've already done it -- it's well outside the scope of this answer (but I'm sure there are other questions that handle it sufficiently on StackOverflow if you look).

Once you have an access token for a particular session (you can get one to test with by going to https://developers.facebook.com/docs/graph-api/reference/v2.3/user/inbox, clicking the Graph Explorer button, clicking "Get Token" -> "Get Access Token", and ensuring that "read_mailbox" is selected under "Extended Permissions), it's pretty straightforward to read the API. You can do it using only standard JDK classes in just a few lines:

    String accessToken = "replaceThisWithAccessToken";
    String urlString = MessageFormat.format("https://graph.facebook.com/v2.3/me/inbox?access_token={0}&&format=json&method=get",
            accessToken);
    URL url = new URL(urlString);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
    }
    bufferedReader.close();

This glosses over a lot of things -- doesn't help with authentication, assumes your active trust store contains a certification path for the Facebook SSL cert (it should), and ignore proper error handling. And in practice you'll want to use RestClient or something similar instead of using URL directly -- but the above should be indicative of basically what you need to do.

Dathan
  • 7,266
  • 3
  • 27
  • 46
  • Thanks Dathan! I looked at the page you provided, The samples were in java script and PHP but not java. Would you please provide some guide on getting this done in java via REST API? – Mohammadreza Jul 05 '15 at 19:21
  • 5
    The `read_mailbox` permission has been deprecated since v2.4 (July 2015) https://developers.facebook.com/docs/apps/changelog#v2_4 and won't return any results from October 6th 2015 onwards. – Antoine Oct 01 '15 at 14:51
  • @AntoineBolvy Isn't there another API to read and send private messages between 2 users? I know bots from some Facebook pages can read and send messages. – baptx Dec 15 '21 at 11:26