首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用restFB发送消息

用restFB发送消息
EN

Stack Overflow用户
提问于 2016-09-19 15:50:10
回答 1查看 1.8K关注 0票数 2

为了在Facebook上发送信息,我尝试了这是一个简单的例子:

代码语言:javascript
复制
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
    }

但我错了

代码语言:javascript
复制
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)

你能提出一些解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2016-10-04 10:33:12

我在两个小时前解决了这个问题:)使用"{conversation_id}/messages“而不是"me/messages”。您可以使用以下代码获得conversation_id:

代码语言:javascript
复制
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() + " ")));
        }
    }

以下代码适用于我:

代码语言:javascript
复制
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!!!")); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39577186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档