How to log out from the Facebook in windows phone 7 using facebook api?

Posted by Vijay on Stack Overflow See other posts from Stack Overflow or by Vijay
Published on 2014-08-25T09:27:01Z Indexed on 2014/08/25 10:20 UTC
Read the original article Hit count: 232

Filed under:
|
|

I am trying to add Facebook into my application. I have try with a sample.

public class FacebookLoginPageViewModel
{
    private static WebBrowser _webBrowser;
    private Page _page;
    private const string ExtendedPermissions = "user_about_me,read_stream,publish_stream,user_birthday,offline_access,email";
    private readonly FacebookClient _fb = new FacebookClient();
    private const string AppId = "1XXX58XXXXXXXX9";
    Uri url;
    public FacebookLoginPageViewModel(Panel container, Page page)
    {
        _page = page;
        _webBrowser = new WebBrowser();

        var loginUrl = GetFacebookLoginUrl(AppId, ExtendedPermissions);
        url = loginUrl;
        container.Children.Add(_webBrowser);
        _webBrowser.Navigated += webBrowser_Navigated;
        _webBrowser.Navigate(loginUrl);
    }

    private Uri GetFacebookLoginUrl(string appId, string extendedPermissions)
    {
        var parameters = new Dictionary<string, object>();
        parameters["client_id"] = appId;
        parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
        parameters["response_type"] = "token";
        parameters["display"] = "touch";

        // add the 'scope' only if we have extendedPermissions.
        if (!string.IsNullOrEmpty(extendedPermissions))
        {
            // A comma-delimited list of permissions
            parameters["scope"] = extendedPermissions;
        }
        return _fb.GetLoginUrl(parameters);
    }

    void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookOAuthResult oauthResult;
        if (!_fb.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
        {
            return;
        }

        if (oauthResult.IsSuccess)
        {
            var accessToken = oauthResult.AccessToken;
            LoginSucceded(accessToken);
        }
        else
        {
            // user cancelled
            MessageBox.Show(oauthResult.ErrorDescription);
        }
    }

    private void LoginSucceded(string accessToken)
    {
        var fb = new FacebookClient(accessToken);

        fb.GetCompleted += (o, e) =>
        {
            if (e.Error != null)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(e.Error.Message);
                    return;
                });
            }

            var result = (IDictionary<string, object>)e.GetResultData();
            var id = (string)result["id"];

            var url = string.Format("/Views/FacebookInfoPage.xaml?access_token={0}&id={1}", accessToken, id);
            var rootFrame = (App.Current as App).RootFrame;
            Deployment.Current.Dispatcher.BeginInvoke(() =>
               {
                   rootFrame.Navigate(new Uri(url, UriKind.Relative));
               });
        };

        fb.GetAsync("me?fields=id");
    }

This is working fine. But i want to Log out from the facebook when i click log out. How to achieve this? I have try with some examples. But it is not working for me.

 private void logout(object sender, RoutedEventArgs e)
{
    webBrowser1.Navigated += 
      new EventHandler<System.Windows.Navigation.NavigationEventArgs>(CheckForout);
    webBrowser1.Navigate(new Uri("http://m.facebook.com/logout.php?confirm=1"));
    webBrowser1.Visibility = Visibility.Visible;
}

private void CheckForout(object sender, System.Windows.Navigation.NavigationEventArgs e)
{

    string fbLogoutDoc = webBrowser1.SaveToString();

    Regex regex = new Regex
    ("\\<a href=\\\"/logout(.*)\\\".*data-sigil=\\\"logout\\\"");
    MatchCollection matches = regex.Matches(fbLogoutDoc);
    if (matches.Count > 0)
    {
        string finalLogout = string.Format("http://m.facebook.com/logout{0}", 
            matches[0].Groups[1].ToString().Replace("amp;", "")); 
         webBrowser1.Navigate(new Uri(finalLogout));
    }
}

Please let me any idea to resolve this problem.

© Stack Overflow or respective owner

Related posts about c#

Related posts about facebook