Csharp/C#教程:整合Facebook聊天分享


整合Facebook聊天

我已经编写了一个程序来集成在C#中的Facebook用户聊天,但是在发送之后我总是得到响应服务器。

我检查了API密钥和App秘密,两者都是正确的。 看起来我将一些错误的参数传递给服务器。

这是我的代码。

 private void GetDetailsButton_Click(object sender, EventArgs e) { TcpClient FacebookClient = new TcpClient(); FacebookClient.Connect("chat.facebook.com", 5222); NetworkStream myns = FacebookClient.GetStream(); string xml = "" + ""; 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 = ""; 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("", ""); myResponseAsSB.Replace("", ""); //converting challenge string to normal string byte[] myregularstrigbytes = Convert.FromBase64String(myResponseAsSB.ToString()); string myregularstring = System.Text.Encoding.UTF8.GetString(myregularstrigbytes); //I've hardcoded the accesstoken here for testing purpose. string SessionKey = AccessToken.Split('|')[1]; string response = ComposeResponse(myregularstring); byte[] myResponseByte = Encoding.UTF8.GetBytes(response.ToString()); string myEncodedResponseToSend = Convert.ToBase64String(myResponseByte); xml = String.Format("{0}", 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); MessageBox.Show(myResponseAsSB.ToString()); } private string ComposeResponse(string serverresponse) { string version = serverresponse.Split('&')[0].Split('=')[1]; string method = serverresponse.Split('&')[1].Split('=')[1]; string nonce = serverresponse.Split('&')[2].Split('=')[1]; string SessionKey = AccessToken.Split('|')[1]; long callId = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; string sig = "api_key=" + appId + "call_id=" + callId + "method=" + method + "nonce=" + nonce + "session_key=" + SessionKey + "v=" + "1.0" + AppSecret; MD5 md = MD5.Create(); var hash = md.ComputeHash(Encoding.UTF8.GetBytes(sig)); sig = hash.Aggregate("", (current, b) => current + b.ToString("x2")); return "api_key=" + HttpUtility.UrlEncode(appId) + "&call_id=" + HttpUtility.UrlEncode(callId) + "&method=" + HttpUtility.UrlEncode(method) + "&nonce=" + HttpUtility.UrlEncode(nonce) + "&session_key=" + HttpUtility.UrlEncode(SessionKey) + "&v=" + HttpUtility.UrlEncode("1.0") + "&sig=" + HttpUtility.UrlEncode(sig); } 

我在C#和X-FACEBOOK-PLATFORM中评论了这篇文章Facebook Chat Authentication ,我的应用程序类型是Native / Desktop。

有人能指出我正确的方向吗?

编辑:我认为问题是在创建签名时,有没有办法validation创建的签名?

编辑1:根据此SO答案 ,访问令牌包含第一个|之后的会话密钥 性格,我可以找到| 直到2天前的角色,但现在我找不到| 访问令牌中的字符,它真的很奇怪,所以我现在如何找到会话密钥? (或者我现在应该去睡觉了。)

编辑2:奇怪的是我总是以||获得本机/桌面应用程序的访问令牌。 我进一步搜索并发现会话密钥需要从auth.promoteSession遗留API中提取,并使用HttpUtility.UrlEncode而不是HttpUtility.HtmlEncode对参数进行编码。

现在我已经硬编码了Access令牌(在访问令牌调试器中validation了它),会话密钥,App密钥和app秘密仍然得到相同的错误

编辑3:我一直在敲打我的头一个星期仍然这不起作用,但今天我发现文档中的更新Note that this needs to be over TLS (Transport Layer Security) or you'll get an error. 我想我需要相应地修改我的代码。

编辑4:我已经尝试了文档中的代码,发现$SESSION_XML的值应该是

 $SESSION_XML = ''. ''; 

转换完后,我会发布C#代码。

要使用X-FACEBOOK-PLATFORM,您将需要随传统身份validation流程提供的用户会话。 但是,access_token包含|之后的用户会话 正如你在edit1中提到的那样。

我们在上一篇博客文章中宣布access_token将被加密,这将是从Octber 1st开始强制执行的。 在此之前,该选项可以在高级应用程序设置https://developers.facebook.com/blog/post/553/中切换。

继续前进,access_token将能够用于X-FACEBOOK-PLATFORM。

如果从现有的XMPP库开始,您将获得更快的速度。 这是一个列表: http : //xmpp.org/xmpp-software/libraries/

例如,您希望您没有手动编码所有XML,而不是很快通过DOM运行它。 在此期间,使用以下字符测试所有输入: <>'"&

在Facebook开发人员网站上有一个Python的例子可能有用: https : //developers.facebook.com/docs/chat/

在尝试使用X-FACEBOOK-PLATFORM进行身份validation之前,我遇到了同样的问题。 我找到了一个我认为可以帮助你的解决方案。

XMPP与Java Asmack库支持X-FACEBOOK-PLATFORM

虽然代码是Java,但很容易理解它是如何工作的。 诀窍是使用您的OAuth 2.0令牌从Facebook获取会话密钥。 您必须以这种方式使用名为auth.promoteSession的弃用方法:

https://api.facebook.com/method/auth.promoteSession?access_token=yourAccessToken

然后,Facebook会为您提供您应该用作应用程序密钥参数的会话密钥。 为了能够使用此方法,您必须在编辑设置部分和高级选项卡中禁用此页面中的删除不推荐使用的API参数。

我希望这可以帮助你。

不知道这是不是问题,我没有时间去测试,但是你的签名看起来不对。 还记得Facebook 在9月初开始转移到OAuth 2.0 ,所以请确保你的access_token是正确的。

试试这个:

上述就是C#学习教程:整合Facebook聊天分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 string sig = "api_key=" + appId + "call_id=" + callId + "method=" + method + "nonce=" + nonce + "session_key=" + SessionKey + "v=1.0" + APP_SECRET; MD5 md = MD5.Create(); var hash = md.ComputeHash(Encoding.UTF8.GetBytes(sig)); sig = hash.Aggregate("", (current, b) => current + b.ToString("x2")); var response = "api_key=" + appId + "&call_id=" + callId + "&method=" + method + "&nonce=" + nonce + "&session_key=" + SessionKey + "&v=1.0"; // response & signature response = string.Concat(Uri.EscapeDataString(response), "&", Uri.EscapeDataString(sig)) // base64 encode var myEncodedResponseToSend = Convert.ToBase64String( ASCIIEncoding.ASCII.GetBytes(response)); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1002428.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐