///////////////// ////////////////////////////////////////////////// ///////////通过Facebook发送信息聊天API(XMPP)C#
// OBSERVE https://developers.facebook.com/docs/chat/
本文档介绍了已弃用平台API 2.0版的发布服务和API。一旦版本1.0被弃用,chat.facebook.com将不再可用。
//阅读此内容,您可能想完成与此问题完全不同的任何操作。
////////////////////////////////////////////// //////////////////////////////////////////
我创建与WebForms C#聊天连接到Facebook Chat API。
我也看了this SO question(和所有链接)。由于Facebook现在需要auth_token
,因此某些部分不再相关。
要复制这个,你应该设置一个Facebook的web应用程序,使用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读取数据。我不知道为什么?
Holy s ...,这是很多的代码。你确定这一切都*必要* *展示*你的问题?我很怀疑。无论如何,这很可能会让你低调,没有答案。 –
@DanielHilgarth嘿嘿嘿..连接的第一部分可能不是必要的,但没有工作的例子,如何在C#中这样做,所以我想我会确保有一个人在那里试图做一个Facebook聊天我C#。我也可以在一个代码库中编写所有代码,但不会非常可监控。 –
如果你想要一个答案,我建议你去掉这篇文章中的代码,只是为了理解这个问题。其他代码(例如,其他人可以重现它),你可以把[pastebin](http://pastebin.com/)并链接到您的帖子。 – Virtlink