我正在尝试将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 ???
答案 0 :(得分:0)
我找到了解决方案。
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注销。