简体   繁体   中英

Facebook chat XMPP authentication with Gloox?

Well, I'm probably doing something silly, but I've been beating my head against the wall with this for the past few hours, and so far I have no idea what I've been doing wrong.

At the moment I'm trying to make this work with PLAIN SASL because it seems like Facebook actively makes OAuth2 a pain for non-Web apps, but it really makes no difference to me as long as I can get this to work somehow.

Current code:

_client = new Client(JID(username /* no @chat.facebook.com */), password);

_client->setServer("chat.facebook.com");
_client->setPort(5222);

_client->setSASLMechanisms(gloox::SaslMechPlain);
_client->setTls(gloox::TLSPolicy::TLSRequired);

_client->connect(false);
_client->login(); // not necessary?

QThread::sleep(10); // arbitrary sleep; should be sufficient

std::cout << _client->authed() << std::endl; // false
std::cout << _client->authError() << std::endl; // AuthErrorUndefined

_client->rosterManager()->fill();

// neither one has any effect
MessageSession(_client, JID("friend@chat.facebook.com")).send("balls");
MessageSession(_client, JID("friend")).send("balls");

std::cout << _client->rosterManager()->roster()->size() << std::endl; // 0

Edit: For that matter, I can't get Gloox working with Gmail either (haven't tried any other XMPP servers).

  1. Your JID is indeed username@chat.facebook.com , not only username - and it is very important to SASL authentication, it will not work with wrong JID.
  2. Facebook chat supports SASL PLAIN authentication over SSL/TLS connection, as well as DIGEST-MD5
  3. Google talk supports SASL PLAIN over TLS too
  4. You can see supported SASL mechanisms in the first <stream:features>...</stream:features> packet from the server
  5. It will be much better if your show error logs

Well, I'm still not 100% sure what the problem with Gloox was, but the following roughly equivalent Swiften code works with no issues.

SimpleEventLoop* eventLoop = new SimpleEventLoop();
BoostNetworkFactories networkFactories(eventLoop);

_client = new Client
(
    username.append("@chat.facebook.com").toStdString(),
    password.toStdString(),
    &networkFactories
);
_client->setAlwaysTrustCertificates();

_client->onConnected.connect([&] () { signInStatus = SignInStatus::Success; });
_client->onDisconnected.connect([&] (const boost::optional<ClientError>& e) {
    signInStatus = SignInStatus::InvalidCredentials;
});

_client->connect();
std::thread([&] () { eventLoop->run(); }).detach();

while (signInStatus == SignInStatus::NotSignedIn)
{
    QThread::sleep(1);
}

if (signInStatus == SignInStatus::InvalidCredentials)
{
    return signInStatus;
}

_client->requestRoster();
QThread::sleep(5);
std::cout << _client->getRoster()->getItems()[0].getName() << std::endl;

Message::ref message(new Message());
message->setTo(_client->getRoster()->getItems()[0].getJID());
message->setFrom(JID());
message->setBody("balls");
_client->sendMessage(message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM