1

我正在尝试使用 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 应用程序 ID、应用程序密码和访问令牌)。但是 Jabber.net 中没有这样的代表!

Jabber.net 上没有 SASL 支持吗?或者你是怎么设置的?

4

1 回答 1

1

jabber-net 中支持 SASL,但不支持 X-FACEBOOK-PLATFORM SASL 机制。如果你想使用这个机制,你必须通过添加这个 SASL 机制来扩展 jabber-net。

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

亚历克斯

于 2012-08-23T06:49:20.347 回答