I am trying to develop an application for Windows Phone 7 using the Facebook login and logout functions. I found the Facebook SDK and used it to log in by opening WebBrowser. The user enters credentials into this browser and successfully logs in. Moreover, I was able to log in without using the SDK, simply using HTTP requests, as the SDK does. However, I want to log out without using WebBrowser, but just by clicking a button, for example. There are so many solutions on the Internet that I have to open a web browser and go to a specific URL in order to log out. However, this is not what I want. I think there should be a way to log out by clearing cookies that I donβt know exactly how to do this, or any others that you offer. Part of my code below:
private static String appID = ""; private static String appSecret = ""; public static void login(String[] permissions) { try { permissionArray = permissions; popup = new Popup(); popup.Height = 480; popup.Width = 480; popup.VerticalOffset = 100; FacebookLoginUserControl control = new FacebookLoginUserControl(); control.facebookWebBrowser.Loaded += new RoutedEventHandler(webBrowser_Loaded); control.facebookWebBrowser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(webBrowser_Navigated); popup.Child = control; popup.IsOpen = true; } catch (Exception e) { //handle } } private static void webBrowser_Loaded(Object sender, RoutedEventArgs e) { WebBrowser wb = (WebBrowser)sender; String loginUrl = GetFacebookLoginUrl(); wb.Navigate(new Uri(loginUrl)); } private static String GetFacebookLoginUrl() { String permissionString = String.Empty; if (permissionArray.Length > 0) permissionString = String.Join(",", permissionArray); var uriParams = new Dictionary<string, string>() { {"client_id", appID}, {"response_type", "token"}, {"scope", permissionString}, {"redirect_uri", "http://www.facebook.com/connect/login_success.html"}, {"display", "touch"} }; StringBuilder urlBuilder = new StringBuilder(); foreach (var current in uriParams) { if (urlBuilder.Length > 0) { urlBuilder.Append("&"); } var encoded = HttpUtility.UrlEncode(current.Value); urlBuilder.AppendFormat("{0}={1}", current.Key, encoded); } var loginUrl = "http://www.facebook.com/dialog/oauth?" + urlBuilder.ToString(); return loginUrl; } private static void webBrowser_Navigated(Object sender, System.Windows.Navigation.NavigationEventArgs e) { if (string.IsNullOrEmpty(e.Uri.Fragment)) return; if (e.Uri.AbsoluteUri.Replace(e.Uri.Fragment, "") == "http://www.facebook.com/connect/login_success.html") { string text = HttpUtility.HtmlDecode(e.Uri.Fragment).TrimStart('#'); var pairs = text.Split('&'); foreach (var pair in pairs) { var kvp = pair.Split('='); if (kvp.Length == 2) { if (kvp[0] == "access_token") { accessToken = kvp[1]; MessageBox.Show("Access granted"); RequestUserProfile(); } } } if (string.IsNullOrEmpty(accessToken)) { MessageBox.Show("Unable to authenticate"); } popup.IsOpen = false; } } private static void RequestUserProfile() { var profileUrl = string.Format("https://graph.facebook.com/me?access_token={0}", HttpUtility.UrlEncode(accessToken)); request = (HttpWebRequest)HttpWebRequest.Create(new Uri(profileUrl)); request.Method = "GET"; request.BeginGetResponse(result => { try { var resp = (result.AsyncState as HttpWebRequest).EndGetResponse(result); using (var strm = resp.GetResponseStream()) { StreamReader sr = new StreamReader(strm); var responseString = sr.ReadToEnd(); } } catch (Exception ex) { // } }, request); }
Any ideas to solve the problem. thanks in advance
source share