我从Facebook C#SDK开始。如何处理官方SDK文档中的消息?我如何阅读和发送消息?有没有教程?
答案 0 :(得分:4)
要访问消息(聊天消息),你必须拥有read_mailbox扩展权限(我认为)并且喜欢:
string facebookToken = "your facebook token here";
var client = new FacebookClient(facebookToken);
dynamic result = client.Get("me/inbox", null);
foreach (dynamic item in result.inbox.data)
{
//item is a conversation
//the latest updated conversations come first so
//im just gona grab the first conversation with unreaded / unseen messages
if (item.unread > 0 || item.unseen > 0)
{
string conversationID = item.id;
string otherPerson = item.to.data[1].name;//the item.to.data[0] its myself
//you can access the messages of the conversation like
//by default it will return the last 25 messages, u can get more, by making a call too
//"https://graph.facebook.com/{0}/comments?limit={1}" like:
//dynamic result = client.Get(string.Format("{0}/comments?limit={1}",conversationID, 100), null);
foreach (dynamic message in item.comments.data)
{
//Do want you want with the messages
string id = message.id;
string fromName = message.from.name;
string fromID = message.from.id;
string text = message.message;
string createdDate = message.created_time;
}
//To send a message in this conversation, just
dynamic parameters = new ExpandoObject();
parameters.message = "A message from code!";
client.Post(string.Format("{0}/comments", conversationID), parameters);
//or
//client.Post(string.Format("{0}/comments", conversationID), new Dictionary<string, object> { { "message", "A message from code!" } });
//NOTE!! - The application must be on white list for you te be able to post a message
// read - https://developers.facebook.com/docs/ApplicationSecurity/
break;
}
}
您可以尝试https://developers.facebook.com/tools/explorer
阅读更多内容:
Inbox Notifications, Message Info
希望它有所帮助;)
答案 1 :(得分:1)
Facebook不允许SDK发送私信以防止垃圾邮件发送者滥用
答案 2 :(得分:1)
这是如何使用c#,asp.net:
显示收件箱protected void Button4_Click(object sender, EventArgs e)
{
var fb = new FacebookClient(lblToken.Text);
var query = string.Format(@"SELECT message_id, author_id, body, created_time FROM message WHERE thread_id IN (SELECT thread_id FROM thread WHERE folder_id = 0)");
dynamic parameters = new ExpandoObject();
parameters.q = query;
dynamic results = fb.Get("/fql", parameters);
List<MyMessage> q = JsonConvert.DeserializeObject<List<MyMessage>>(results.data.ToString());
GridView4.DataSource = q;
GridView4.DataBind();
}
答案 3 :(得分:0)
这是用于发送消息..
使用Facebook C#SDK(http://facebooksdk.codeplex.com)
var app = new FacebookApp("access_token");
var parameters = new Dictionary<string, object>();
parameters["message"] = "This is a test message";
app.Api("/me", parameters, HttpMethod.Post);
这会将消息发布到当前用户的墙上。您也可以使用该SDK发布图像。测试中有样本如何做到这一点。请注意,如果您的意思是您希望向他们发送私人消息,而不是在他们的墙上发布这是不可能的。 Facebook不允许应用程序直接向用户发送消息。