3

我正在尝试使用 C# 连接到 Facebook 聊天。

我能够获取访问代码和访问令牌,但是当我们尝试对聊天进行身份验证时它最终失败了,它给出了一个错误,说它没有经过身份验证。

我想我搞错了ComposeResponse。请帮我。:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = Request.Url.AbsoluteUri;

        if (Request.QueryString["code"] == null)
        {
            Response.Redirect(GetFacebookOAuthUrl());
        }

        var Demo = GetMAccessToken(Request.QueryString["code"]);

        TcpClient FacebookClient = new TcpClient();
        FacebookClient.Connect("chat.facebook.com", 5222);
        NetworkStream myns = FacebookClient.GetStream();

        string xml = "<?xml version='1.0'?>" +
        "<stream:stream " +
        "id='1' " +
        "to='chat.facebook.com' " +
        "xmlns='jabber:client' " +
        "xmlns:stream='http://etherx.jabber.org/streams' " +
        "version='1.0' >";

        StreamWriter mySw = new StreamWriter(myns);
        mySw.WriteLine(xml);  //sending initial request
        mySw.Flush();

        byte[] serverResponseByte = new byte[1024];
        int myBytesRead = 0;
        StringBuilder myResponseAsSB = new StringBuilder();

        //reading response from the server to see the supported authentication methods 
        do
        {
            myBytesRead = myns.Read(serverResponseByte, 0, serverResponseByte.Length);
            myResponseAsSB.Append(System.Text.Encoding.UTF8.GetString(serverResponseByte, 0, myBytesRead));

        } while (myns.DataAvailable);

        myResponseAsSB.Clear();

        xml = "<auth " +
        "xmlns='urn:ietf:params:xml:ns:xmpp-sasl' " +
        "mechanism='X-FACEBOOK-PLATFORM'  />";

        mySw.WriteLine(xml);
        mySw.Flush();   //sending response to server to use X-FACEBOOK-PLATFORM

        //reading challenge send by the server
        do
        {
            myBytesRead = myns.Read(serverResponseByte, 0, serverResponseByte.Length);
            myResponseAsSB.Append(System.Text.Encoding.UTF8.GetString(serverResponseByte, 0, myBytesRead));

        } while (myns.DataAvailable);

        myResponseAsSB.Replace("<challenge xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">", "");
        myResponseAsSB.Replace("</challenge>", "");

        //converting challenge string to normal string
        byte[] myregularstrigbytes = Convert.FromBase64String(myResponseAsSB.ToString());
        string myregularstring = System.Text.Encoding.UTF8.GetString(myregularstrigbytes);

        string response = ComposeResponse(myregularstring, Demo);

        byte[] myResponseByte = Encoding.UTF8.GetBytes(response.ToString());

        string myEncodedResponseToSend = Convert.ToBase64String(myResponseByte);
        xml = String.Format("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">{0}</response>", myEncodedResponseToSend);
        mySw.WriteLine(xml);
        mySw.Flush();   //sending the response to the server with my parameters

        myResponseAsSB.Clear();

        //checking if authentication succeed 
        do
        {
            myBytesRead = myns.Read(serverResponseByte, 0, serverResponseByte.Length);
            myResponseAsSB.Append(System.Text.Encoding.UTF8.GetString(serverResponseByte, 0, myBytesRead));

        } while (myns.DataAvailable);

        // It fails here..
        string result_ = myResponseAsSB.ToString();
    }

    private string ComposeResponse(string serverresponse, string AccessToken)
    {
        string version = serverresponse.Split('&')[0].Split('=')[1];
        string method = serverresponse.Split('&')[1].Split('=')[1];
        string nonce = serverresponse.Split('&')[2].Split('=')[1];
        string appId = "421025367912155";

        string Response = "api_key=" + appId
        + "&call_id=" + 0
        + "&method=" + method
        + "&nonce=" + nonce
        + "&access_token=" + AccessToken
        + "&v='1.0'";

        return Response;
    }

    public string GetMAccessToken(string Code)
    {
        string AccessToken;

        string RequestToken = "https://graph.facebook.com/oauth/access_token?" +
        "client_id=421025367912155" +
        "&redirect_uri=http://localhost:2155/FBintegration/" +
        "&client_secret=10d4cfc72892a8c291ff0a8f6fc0003f" +
        "&code=" + Code ;

        WebRequest req = WebRequest.Create(RequestToken);

        WebResponse Response = req.GetResponse();

        Stream dataStream = Response.GetResponseStream();

        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();

        string[] Res = responseFromServer.Split(new char[] {'&'},StringSplitOptions.RemoveEmptyEntries);

        AccessToken = Res[0];

        AccessToken = AccessToken.Replace("access_token=", "");

        return AccessToken;
    }

    public string GetFacebookOAuthUrl()
    {
         string RedirectUrl = "https://www.facebook.com/dialog/oauth?scope=xmpp_login" +
            "&client_id=421025367912155" +
            "&redirect_uri=" + new System.Uri("http://localhost:2155/FBintegration/");

         return RedirectUrl;
    }
}
4

0 回答 0