使用带有SASL的Jabber.net(C#/ Mono)连接到Facebook聊天

时间:2012-08-22 08:46:40

标签: c# xmpp facebook-chat sasl

我正在尝试使用Jabber.net(c#/ Mono)连接到Facebook聊天。我正在使用的代码是:

    static ManualResetEvent done = new ManualResetEvent(false);

    const bool VERBOSE = true;
    const string TARGET = "friends_username@chat.facebook.com";

    void Start () 
    {

        JabberClient j = new JabberClient();

        j.User = "my_facebook_alias";  //Later to be removed
        j.Server = "chat.facebook.com"; 
        j.Port = 5222;
        j.NetworkHost = "chat.facebook.com";
        j.AutoStartCompression = true;
        j.KeepAlive = 30F;
        j.AutoStartTLS = false;
        j.AutoStartCompression = true;
        j.PlaintextAuth = true;
        j.Password = "my_facebook_password";  //Later to be removed
        j.RequiresSASL = true;
        j.LocalCertificate = null;
        // don't do extra stuff, please.
        j.AutoPresence = false;
        j.AutoRoster = true;
        j.AutoReconnect = -1;

        j.OnInvalidCertificate += new System.Net.Security.RemoteCertificateValidationCallback(j_OnInvalidCertificate);

        // listen for errors.  Always do this!
        j.OnError += new bedrock.ExceptionHandler(j_OnError);

        // what to do when login completes
        j.OnAuthenticate += new bedrock.ObjectHandler(j_OnAuthenticate);

        // listen for XMPP wire protocol
        if (VERBOSE)
        {
            j.OnReadText += new bedrock.TextHandler(j_OnReadText);
            j.OnWriteText += new bedrock.TextHandler(j_OnWriteText);
        }

        // Set everything in motion
        j.Connect();

        // wait until sending a message is complete
        done.WaitOne();

        // logout cleanly
        j.Close();
    }

    static void j_OnWriteText(object sender, string txt)
    {
        if (txt == " ") return;  // ignore keep-alive spaces
        Console.WriteLine("SEND: " + txt);
    }

    static void j_OnReadText(object sender, string txt)
    {
        if (txt == " ") return;  // ignore keep-alive spaces
        Console.WriteLine("RECV: " + txt);
    }

    static void j_OnAuthenticate(object sender)
    {
        Debug.Log ("OnAuth...");
        // Sender is always the JabberClient.
        JabberClient j = (JabberClient)sender;
        j.Message(TARGET, "My test message!!!");

        // Finished sending.  Shut down.
        done.Set();
    }

    static void j_OnError(object sender, Exception ex)
    {
        // There was an error!
        Console.WriteLine("Error: " + ex.ToString());
        Debug.Log ("Error: "+ex.ToString());
        // Shut down.
        done.Set();
    }

    bool j_OnInvalidCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        Debug.Log("Invalid certificate "+sslPolicyErrors.ToString() +" : "+certificate.ToString(true));

        return true;
    }

我得到j_OnAuthenticate哪个好。并且它使用我的id /密码,因为将密码更改为其他内容会出现错误。但是我确实想使用X-FACEBOOK-PLATFORM SASL机制来登录而不保存用户ID /密码。

完整的日志是:

RECV: <?xml version="1.0"?><stream:stream id="E34678B6" from="chat.facebook.com" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" xml:lang="en">
RECV: <stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-FACEBOOK-PLATFORM</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms></stream:features>
SEND: <auth mechanism="DIGEST-MD5" xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />
RECV: <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09ImNoYXQuZmFjZWJvb2suY29tIixub25jZT0iMDBENjAwQ0UyNjZBNzgyMEE5MUIzRDhGNjZDODQ1QjMiLHFvcD0iYXV0aCIsY2hhcnNldD11dGYtOCxhbGdvcml0aG09bWQ1LXNlc3M=</challenge>
SEND: <response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">dXNlcm5hbWU9ImpvbmFzMiIscmVhbG09ImNoYXQuZmFjZWJvb2suY29tIixub25jZT0iMDBENjAwQ0UyNjZBNzgyMEE5MUIzRDhGNjZDODQ1QjMiLGNub25jZT0iMzUzMzMyM2E2YTZmNmU2MTczMzIzYTUxNGU1ZjQxNjI0YzM4NjYiLG5jPTAwMDAwMDAxLHFvcD1hdXRoLGRpZ2VzdC11cmk9InhtcHAvY2hhdC5mYWNlYm9vay5jb20iLHJlc3BvbnNlPTcxN2ExNjM0MzdjOWU5MDM2NDcwYjViNTBhOTYxODIxLGNoYXJzZXQ9dXRmLTg=</response>
RECV: <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cnNwYXV0aD1hM2ZlZGM1NGRmYzNmMGEzMTU5Y2EyZDVmMmVkMmE2Zg==</challenge>
SEND: <response xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />
RECV: <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
SEND: <stream:stream xmlns:stream="http://etherx.jabber.org/streams" id="10996e3a" xmlns="jabber:client" to="chat.facebook.com" version="1.0">
RECV: <?xml version="1.0"?><stream:stream id="2AF2D483" from="chat.facebook.com" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" xml:lang="en">
RECV: <stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>
SEND: <iq id="JN_1" type="set" to="chat.facebook.com"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>Jabber.Net</resource></bind></iq>
RECV: <iq id="JN_1" type="result"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>jonas2@chat.facebook.com/Jabber.Net_c938c6a2_4C7D6EF9DCFDC</jid></bind></iq>
SEND: <iq id="JN_2" type="set" to="chat.facebook.com"><session xmlns="urn:ietf:params:xml:ns:xmpp-session" /></iq>
RECV: <iq type="result" from="chat.facebook.com" id="JN_2"><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq>
OnAuth...
SEND: <message id="JN_3" type="chat" to="friends_username@chat.facebook.com"><body>My test message!!!</body></message>
SEND: <presence type="unavailable"><status>offline</status></presence>
SEND: </stream:stream>
SEND: <iq id="JN_4" type="get"><query xmlns="jabber:iq:roster" /></iq>

在MatriX XMPP中有一个OnBeforeSasl委托,其中自定义方法可以设置所需的Facebook Sasl属性(如facebook app id,app secret和access token)。但是在Jabber.net中没有这样的代表!

Jabber.net是否没有SASL支持?或者你如何设置它?

1 个答案:

答案 0 :(得分:1)

在jabber-net中有SASL支持,但是不支持X-FACEBOOK-PLATFORM SASL机制。如果要使用此机制,则必须通过添加此SASL机制来扩展jabber-net。

Facebook正在关闭XMPP连接。这可能与您在Authenticate事件中发送的消息有关。尝试在jabber-net发送您的初始状态后,或者在您获得名单后发送消息。

亚历