注意:https://developers.facebook.com/docs/chat/中所述的服务和API已经随着Platform API v2.0的发布而被弃用。一旦版本1.0被弃用,chat.facebook.com将不再可用。
重要提示!请务必阅读此内容,您可能希望执行完全不同于与此问题有关的任何操作。
我正在使用WebForms C#创建一个与Facebook Chat API连接的聊天应用程序。
我还查看了这个SO问题(以及所有链接)。由于Facebook现在需要auth_token
,因此某些部分已不再相关。
为了复制这个过程,您应该设置一个Facebook Web应用程序,使用appId
和具有xmpp_login权限设置的用户帐户。然后创建一个Chat.aspx
和与之相应的代码。并替换要交互的硬编码用户。
我有两个(也许三个)问题,我认为这些问题会妨碍我实现发送聊天消息的目标。
- 在文档中标注为
// finishes auth process
的过程与文档描述不符(在我从Facebook接收到基于SSL/TLS的成功消息之后,我没有收到任何响应)。 - 我不知道“发送聊天消息”部分应该如何设置,并且由于我没有接收到来自Facebook的任何消息,因此很难确定可能出了什么问题。
我还有一些帮助程序用于添加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字节。
尝试发送消息也会从Facebook中读取到0字节的数据。我不知道为什么?