0

我正在尝试使用 node.js 和 node-xmpp 连接到 GTalk。node-xmpp 没有成功连接,而是返回一个 XML 错误响应。

准备

$node -v
v0.10.24
$npm install node-stringprep
$npm install node-xmpp
$npm list
...
├─┬ node-xmpp@0.12.2
...

gtalk.js

var xmpp = require('node-xmpp');
var sys = require("sys");

var options = {
    jid: "mygoogleid@gmail.com",
    password: "mygooglepassword"
    //,host: "talk.google.com"
    //,port: 5222
};

var jabber = new xmpp.Client(options);

jabber.on('online', function() {
    console.log("Connected");
});

jabber.on('error', function(e) {
    sys.puts(e);
});

错误

$node gtalk.js
<stream:error xmlns:stream="http://etherx.jabber.org/streams"><invalid-xml xmlns="urn:ietf:params:xml:ns:xmpp-streams"/></stream:error>

可能的解释

  1. ID/密码错误(在谷歌网站登录确实有效)
  2. node-xmpp 需要主机/端口才能连接到 GTalk(尝试过 talk.google.com/5222)
  3. talk.google.com@5222是错误的(Google Doc Open Com说它是正确的)
  4. GTalk 仅允许 OAuth2Google Doc Open Com说“旧”设备允许使用 SASL PLAIN)
  5. 这与StackOverflow#4349577相同(没有相同的错字)
  6. 我使用 node-xmpp 错误
  7. GTalk 未按预期响应
  8. node-xmpp 有问题/不支持 GTalk

我很确定排除了 1-5,我很高兴听到 6-8 的其他解释或证据。谢谢!

更新

1. XML Stream ("=>" outbound, "<=" inbound)

=> <stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="gmail.com">
<= <stream:stream from="gmail.com" id="C2AA8A4648B596A1" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"><stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-OAUTH2</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism></mechanisms></stream:features>
=> <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
<= <proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
=> <stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="gmail.com">
<= <stream:stream from="gmail.com" id="3D42C6DC5985A9E6" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client">
<= <stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-OAUTH2</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism><mechanism>PLAIN</mechanism></mechanisms></stream:features>
=> <auth auth:service="oauth2" xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-OAUTH2">AGNvaW2iaXRzeUBnbWFpbC5jb12AdW5kZWZpbmVk</auth>
<= <stream:error><invalid-xml xmlns="urn:ietf:params:xml:ns:xmpp-streams"/></stream:error>

尽管我没有提供任何令牌,但似乎 node-xmpp 尝试使用 OAUTH2。它应该使用 PLAIN 代替。

2. node-xmpp oauth 示例也不起作用

根据 node-xmpp pull request #85 创建了 OAUTH2 令牌

$node node_modules/node_xmpp/examples/echo_bot_oauth.js gmailid@gmail.com oauth2token
No usable SASL mechanism

3.增加preferred:"PLAIN"修复连接问题

将选项更改为

 var options = {
   jid: "mygoogleid@gmail.com",
   password: "mygooglepassword",
   preferred: "PLAIN"
 };

修复了错误,似乎验证成功。这可能是解释 #8 的证据(node-xmpp 中的错误)。

4

2 回答 2

0

您发送的 XML 确实无效:

<auth auth:service="oauth2"
      xmlns="urn:ietf:params:xml:ns:xmpp-sasl"
      mechanism="X-OAUTH2">
  ...
</auth>

它使用auth命名空间前缀而不在任何地方定义它。

Google 对 OAuth 授权的描述有一个包含命名空间声明(xmlns:auth属性)的示例:

<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl"
    mechanism="X-OAUTH2"
    auth:service="oauth2"
    xmlns:auth="http://www.google.com/talk/protocol/auth">
  base64("\0" + user_name + "\0" + oauth_token)
</auth>

所以这可能应该作为一个错误报告给 node-xmpp 开发人员。

于 2014-01-22T16:48:22.123 回答
0

这是 0.10.9 中引入的 node-xmpp 中的一个错误。

于 2014-01-23T14:37:59.817 回答