2013-10-25 109 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