title: Send message via Facebook Chat API (XMPP) C#
body: <blockquote>
    <p><strong>OBSERVE</strong> <a href="https://developers.facebook.com/docs/chat/" rel="noreferrer">https://developers.facebook.com/docs/chat/</a></p>
    
    <p>The service and API this document covers has been deprecated with the release of Platform API v2.0. Once version 1.0 is deprecated, chat.facebook.com will no longer be available.</p>
    
    <p><strong>Important! Read this and you probably want to do something completely different than anything that has to do with this question.</strong></p>
  </blockquote>
  
  <p>I'm creating a chat with WebForms C# connecting to Facebook Chat API. </p>
  
  <p>I have also looked at <a href="https://stackoverflow.com/q/7221622/146622">this SO question</a> (and all links). Some parts are no longer relevant since Facebook requires <code>auth_token</code> now.</p>
  
  <p>To replicate this, you should set up a Facebook web app, use the <code>appId</code> and a user account with xmpp_login permission set. Then create a <code>Chat.aspx</code> with code behind and paste this code accordingly. And replace the hard-coded users to interact with.</p>
  
  <p>I have two (maybe three) issues which I believe prevent me from succeeding with my goal to send a chat message. </p>
  
  <ol>
  <li>The process noted as <code>// finishes auth process</code> in the documentation does not match the <a href="https://developers.facebook.com/docs/chat/" rel="noreferrer">documentation description</a>
  (I'm not getting any respones after I have received my SSL/TLS based success message from Facebook.)</li>
  <li>I have no idea how the 'send chat message'-part should be set up, and since I don't receive any messages from Facebook its hard to tell what might be wrong.</li>
  </ol>
  
  <p><a href="http://pastebin.com/jst7qkrh" rel="noreferrer">Here is my code in its entirety, on PasteBin</a>.</p>
  
  <p>I also have some helpers for adding xmpp_login permissions and such.. removed for clarity.</p>
  
  <p>Global variables:</p>
  
  <pre><code>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 = "&lt;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\"&gt;";
      private const string AUTH_XML = "&lt;auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-FACEBOOK-PLATFORM'&gt;&lt;/auth&gt;";
      private const string CLOSE_XML = "&lt;/stream:stream&gt;";
      private const string RESOURCE_XML = "&lt;iq type=\"set\" id=\"3\"&gt;&lt;bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"&gt;&lt;resource&gt;fb_xmpp_script&lt;/resource&gt;&lt;/bind&gt;&lt;/iq&gt;";
      private const string SESSION_XML = "&lt;iq type=\"set\" id=\"4\" to=\"chat.facebook.com\"&gt;&lt;session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/&gt;&lt;/iq&gt;";
      private const string START_TLS = "&lt;starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/&gt;";
  </code></pre>
  
  <p>Then in <code>Page_Load</code> all the steps required are (or are supposed to be) performed. Worth noting is the <code>SendMessage("test");</code>. I just tried to put it there to see if it would succeed in sending a chat message... <code>SetUserNameAndAuthToken</code> sets my auth token and user name to global variables. The AuthToken works. </p>
  
  <pre><code>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);
  
  }
  </code></pre>
  
  <p>So I get a response then I send the response to server:</p>
  
  <pre><code>private void SendResponseToServer(string response)
  {
      string xml = String.Format("&lt;response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"&gt;{0}&lt;/response&gt;", response);
      string response2 = SendWihSsl2(xml);
      if (!response2.ToLower().Contains("success"))
          _error = response2;
  }
  </code></pre>
  
  <p>This takes 1 minute 40 seconds... and response is:</p>
  
  <pre><code>&lt;success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/&gt;
  </code></pre>
  
  <p>Finally I do the FinishAuthPorcess()</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>All responses are <code>""</code>. Looking at the <code>Read</code> method in <code>SendWithSsl</code>: it's 0 bytes.
  Trying to send a message also gives me 0 bytes Read data from Facebook. I have no clue as to why?</p>