gpt4 book ai didi

java - 使用 restFB 发送消息

转载 作者:行者123 更新时间:2023-11-30 10:38:48 26 4
gpt4 key购买 nike

我试过这个简单的例子,以便在 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
}

但是我得到错误

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)

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

最佳答案

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

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() + " ")));
}
}

以下代码适合我:

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!!!"));

关于java - 使用 restFB 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39577186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com