如何使用 Facebook C# SDK 阅读/发送私人消息?

Dhi*_*chi 1 .net c# windows-phone-7 facebook-c#-sdk

我从 Facebook C# SDK 开始。SDK官方文档中的消息如何处理?如何阅读和发送消息?有教程吗?

Síl*_* N. 5

要访问消息(聊天消息),您必须拥有 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在https://developers.facebook.com/tools/explorer尝试

阅读更多内容:

收件箱通知消息信息

变化:即将发生变化

希望它有所帮助;)