Mohammadreza Mohammadreza - 5 months ago 133
Java Question

How to use Facebook Messenger API?

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.


You can use the inbox resource of the Graph API:


In order to use this from Java, you'll need to first follow the login instructions at . 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, 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("{0}&&format=json&method=get",
    URL url = new URL(urlString);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    while ((line = bufferedReader.readLine()) != null) {

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.