如何在windowsphone 7中使用facebookapi从Facebook注销

本文关键字:facebookapi Facebook 注销 windowsphone | 更新日期: 2023-09-27 18:19:28

我正在尝试将Facebook添加到我的应用程序中。我试过一个样品。

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");
    }

这很好用。但是当我点击注销时,我想从facebook注销。如何做到这一点?我试着举了一些例子。但这对我不起作用。

 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));
    }
}

请告诉我解决这个问题的办法。

我还有一个例子:

https://www.facebook.com/logout.php?next=YOUR_URL&access_token=ACCESS_TOKEN

我试过这样做:

string newURL = string.Format("https://www.facebook.com/logout.php?next={0}&access_token={1}", _userId, _accessToken);

但在这里,我必须向YOUR_URL传递什么???

如何在windowsphone 7中使用facebookapi从Facebook注销

我找到了解决方案。

public static void logoutSession()
        {
            _webBrowser.Navigated +=  new EventHandler<System.Windows.Navigation.NavigationEventArgs>(CheckForout);
            string logoutUrl = "https://www.facebook.com/connect/login_success.html";
            string newURL = string.Format("https://www.facebook.com/logout.php?next={0}&access_token={1}", logoutUrl, access_tocken);
            _webBrowser.Navigate(new Uri(newURL));
        }
        public static void CheckForout(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            string fbLogoutDoc = _webBrowser.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;", ""));
                _webBrowser.Navigate(new Uri(finalLogout));
            }
        }

这将从windows phone 7中的facebook注销。