【发布时间】:2013-11-04 15:00:43
【问题描述】:
观察 https://developers.facebook.com/docs/chat/
本文档涵盖的服务和 API 在 Platform API v2.0 版本中已弃用。一旦版本 1.0 被弃用,chat.facebook.com 将不再可用。
重要!阅读本文,您可能想做一些与这个问题完全不同的事情。
我正在使用连接到 Facebook 聊天 API 的 WebForms C# 创建聊天。
我还查看了this SO question(以及所有链接)。由于 Facebook 现在需要 auth_token,因此某些部分不再相关。
要复制这一点,您应该设置一个 Facebook 网络应用程序,使用 appId 和一个具有 xmpp_login 权限集的用户帐户。然后创建一个带有代码的Chat.aspx 并相应地粘贴此代码。并替换硬编码的用户进行交互。
我有两个(可能是三个)问题,我认为这会阻止我成功实现发送聊天消息的目标。
- 文档中标记为
// finishes auth process的过程与documentation description 不匹配 (从 Facebook 收到基于 SSL/TLS 的成功消息后,我没有收到任何回复。) - 我不知道应该如何设置“发送聊天消息”部分,而且由于我没有收到来自 Facebook 的任何消息,因此很难判断可能出了什么问题。
Here is my code in its entirety, on PasteBin.
我还有一些用于添加 xmpp_login 权限等的助手。为清楚起见,将其删除。
全局变量:
public partial class Chat : Page
{
public TcpClient client = new TcpClient();
NetworkStream stream;
private SslStream ssl;
private string AppId { get; set; }
public string AppSecret { get; set; }
public string AppUrl { get; set; }
public string UserId { get; set; }
public string AccessToken { get; set; }
private string _error = string.Empty;//global error string for watch debugging in VS.
public const string FbServer = "chat.facebook.com";
private const string STREAM_XML = "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:client\" to=\"chat.facebook.com\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">";
private const string AUTH_XML = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-FACEBOOK-PLATFORM'></auth>";
private const string CLOSE_XML = "</stream:stream>";
private const string RESOURCE_XML = "<iq type=\"set\" id=\"3\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><resource>fb_xmpp_script</resource></bind></iq>";
private const string SESSION_XML = "<iq type=\"set\" id=\"4\" to=\"chat.facebook.com\"><session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/></iq>";
private const string START_TLS = "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
然后在Page_Load 中执行(或应该执行)所有必需的步骤。值得注意的是SendMessage("test");。我只是试着把它放在那里看看它是否能成功发送聊天消息......SetUserNameAndAuthToken 将我的身份验证令牌和用户名设置为全局变量。 AuthToken 有效。
protected void Page_Load(object sender, EventArgs e)
{
this.AppId = "000000082000090";//TODO get from appsettings.
//AddAdditionalPermissions("xmpp_login");//TODO handle xmpp_login persmission
this.AppSecret = "d370c1bfec9be6d9accbdf0117f2c495"; //TODO Get appsecret from appsetting.
this.AppUrl = "https://fbd.anteckna.nu";
SetUserNameAndAuthToken();
Connect(FbServer);
// initiates auth process (using X-FACEBOOK_PLATFORM)
InitiateAuthProcess(STREAM_XML);
// starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
StartTlsConnection(START_TLS);
// gets decoded challenge from server
var decoded = GetDecodedChallenge(AUTH_XML);
// creates the response and signature
string response = CreateResponse(decoded);
//send response to server
SendResponseToServer(response);
SendMessage("test");
// finishes auth process
FinishAuthProcess();
// we made it!
string streamresponseEnd = SendWihSsl(CLOSE_XML);
}
所以我得到一个响应然后我将响应发送到服务器:
private void SendResponseToServer(string response)
{
string xml = String.Format("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">{0}</response>", response);
string response2 = SendWihSsl2(xml);
if (!response2.ToLower().Contains("success"))
_error = response2;
}
这需要 1 分 40 秒...响应是:
<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
最后我做了 FinishAuthPorcess()
private void FinishAuthProcess()
{
string streamresponse = SendWithSsl(STREAM_XML);
if (!streamresponse.Contains("STREAM:STREAM"))
_error = streamresponse;
string streamresponse2 = SendWihSsl(RESOURCE_XML);
if (!streamresponse2.Contains("JID"))
_error = streamresponse2;
string streamresponse3 = SendWihSsl(SESSION_XML);
if (!streamresponse3.Contains("SESSION"))
_error = streamresponse2;
}
所有回复都是""。查看SendWithSsl 中的Read 方法:它是0 字节。
尝试发送消息也会给我 0 字节从 Facebook 读取数据。我不知道为什么?
【问题讨论】:
-
@DanielHilgarth 呵呵,好吧.. Connect 的第一部分可能不是必需的,但是没有关于如何在 C# 中执行此操作的有效示例,所以我想我会确保有一个适合任何人在那里尝试使用 C# 进行 facebook 聊天。我也可以在一个代码库中编写所有内容,但它不会很容易监督。
-
如果你想要一个答案,我建议你把这篇文章中的代码删减到理解问题所需的最低限度。其余代码(例如让其他人能够复制它)您可以输入pastebin 并从您的帖子中链接到它。
-
您的代码不完整,所以我无法遵循控制流程。请附上调试 XML,发送到服务器的内容以及返回的内容。你为什么不使用现有的 XMPP 库来为你工作? xmpp.org/xmpp-software/libraries
-
@MatiCicero 根据 facebook,chat.facebook.com xmpp 服务器将在 6 个月后关闭。
-
@cari 你不应该使用这个解决方案。请改用 agsXMPP 之类的库。但是,所有这些解决方案都将在 Facebook 关闭聊天服务器后的六个月内停止工作。
标签: c# facebook xmpp facebook-chat