2

I tried this is simple example in order to send message in Facebook:

public void testSomeMethod()
    {
        String token = "XXXXXX";
        FacebookClient facebookClient = new DefaultFacebookClient(token, Version.LATEST);


        IdMessageRecipient recipient = new IdMessageRecipient("123456");
        Message message = new Message("Just a simple text");

        SendResponse resp = facebookClient.publish("me/messages", SendResponse.class,
            Parameter.with("recipient", recipient), // the id or phone recipient
            Parameter.with("message", message)); // one of the messages from above
    }

But I get error

com.restfb.exception.FacebookGraphException: Received Facebook error response of type GraphMethodException: Unsupported post request. Object with ID 'me' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api (code 100, subcode null)
    at com.facebook.impl.FacebookImplTest.testSomeMethod(FacebookImplTest.java:57)

Can you propose some solution?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

1

I solved this problem two hour ago :) Use "{conversation_id}/messages" instead of "me/messages". You can get conversation_id using the following code:

Connection<Conversation> conversations = pageClient.fetchConnection("me/conversations", Conversation.class);
    for (List<Conversation> conversationPage : conversations) {
        for (Conversation conversation : conversationPage) {
            String id = conversation.getId();   //use this conversation_id
            Connection<Message> messages = pageClient.fetchConnection(id + "/messages", Message.class, Parameter.with("fields", "message,created_time,from,id"));
            messages.forEach(s -> s.forEach(k -> System.out.println(k.getFrom() + " " + k.getId() + " " + k.getMessage() + " " + k.getSubject() + " ")));
        }
    }

The following code works properly for me:

FacebookClient pageClient = new DefaultFacebookClient(pageAccessToken, Version.VERSION_2_6);
    IdMessageRecipient recipient = new IdMessageRecipient("{user_id}");      
    String conversationId = “t_mid.14XXX.. : XXX...” ;         
    SendResponse resp = pageClient.publish(conversationId +"/messages", SendResponse.class,                
            Parameter.with("recipient", recipient),             
            Parameter.with("message", "Uraaaa!!!"));