2013-10-25 73 views
37

///////////////// ////////////////////////////////////////////////// ///////////通过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并相应地粘贴此代码。并替换硬编码的用户进行交互。

我有我相信阻止我与我的目标是发送聊天消息,随后的两个(也许三)的问题。

  1. 过程文档中标注为// finishes auth process不匹配documentation description (我没有收到任何阶跃响应我已收到来自Facebook的我的SSL/TLS基于成功的消息后。)
  2. 我不知道应该如何设置“发送聊天消息”部分,并且由于我没有收到任何来自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读取数据。我不知道为什么?

+13

Holy s ...,这是很多的代码。你确定这一切都*必要* *展示*你的问题?我很怀疑。无论如何,这很可能会让你低调,没有答案。 –

+3

@DanielHilgarth嘿嘿嘿..连接的第一部分可能不是必要的,但没有工作的例子,如何在C#中这样做,所以我想我会确保有一个人在那里试图做一个Facebook聊天我C#。我也可以在一个代码库中编写所有代码,但不会非常可监控。 –

+2

如果你想要一个答案,我建议你去掉这篇文章中的代码,只是为了理解这个问题。其他代码(例如,其他人可以重现它),你可以把[pastebin](http://pastebin.com/)并链接到您的帖子。 – Virtlink

回答

-2

我会去一个已经做好的jabber/xmpp库,并添加Facebook认证的东西。我之前写过自己的PHP,但不是C#。尽管在几个月内FB Chat API将会死亡(真的很蹩脚),当我搜索周围时,我发现了很多C#示例(我编码)。下面是如何使一个聊天客户端与注释的源视频:https://www.youtube.com/watch?v=tHOb80TghDA

这里是一个文章的示例应用程序的完整的源代码:http://csharp-tricks-en.blogspot.com/2013/10/connect-to-facebook-chat.html

这里的专为Facebook聊天API一个NuGet包XMPP库:https://www.nuget.org/packages/Facebook-Chat/

如果您检查一些示例的来源机会,您会看到哪里出错或者您可以从其中一个示例开始。最后,我进行的搜索:https://www.google.com/search?q=c%23+chat+api+library&ie=utf-8&oe=utf-8#safe=off&q=c%23+facebook+chat+api+library

+0

由于chat.facebook.com服务器将在几个月内脱机,因此这不再是问题。 –

+0

目前这个问题还没有解决。考虑到很多人可能仍然使用这种方式来赚钱,并将继续这样做,直到它被取消。 – xendi