Send message via Facebook Chat API (XMPP) C#

﹥>﹥吖頭↗ 提交于 2019-12-03 08:05:56

问题


//////////////////////////////////////////////////////////////////////////////////////////////////

// OBSERVE https://developers.facebook.com/docs/chat/

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.

// Read this and you probably want to do something completely different than anything that has to do with this question.

////////////////////////////////////////////////////////////////////////////////////////

I'm creating a chat with WebForms C# connecting to Facebook Chat API.

I have also looked at this SO question (and all links). Some parts are no longer relevant since Facebook requires auth_token now.

To replicate this, you should set up a Facebook web app, use the appId and a user account with xmpp_login permission set. Then create a Chat.aspx with code behind and paste this code accordingly. And replace the hard-coded users to interact with.

I have two (maybe three) issues which I believe prevent me from succeeding with my goal to send a chat message.

  1. The process noted as // finishes auth process in the documentation does not match the documentation description (I'm not getting any respones after I have received my SSL/TLS based success message from Facebook.)
  2. 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.

Here is my code in its entirety, on PasteBin.

I also have some helpers for adding xmpp_login permissions and such.. removed for clarity.

Global variables:

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\"/>";

Then in Page_Load all the steps required are (or are supposed to be) performed. Worth noting is the SendMessage("test");. I just tried to put it there to see if it would succeed in sending a chat message... SetUserNameAndAuthToken sets my auth token and user name to global variables. The AuthToken works.

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);

}

So I get a response then I send the response to server:

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;
}

This takes 1 minute 40 seconds... and response is:

<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>

Finally I do the 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;
}

All responses are "". Looking at the Read method in SendWithSsl: 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?

来源:https://stackoverflow.com/questions/19590752/send-message-via-facebook-chat-api-xmpp-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!