问题
I think I've read just about everything out there on the topic of logging out of Facebook inside of a Desktop application. Nothing so far works.
Specifically, I would like to log the user out so that they can switch identities, e.g. People sharing a computer at home could then use the software with their own Facebook accounts, but with no chance to switch accounts, it's quite messy. (Have not yet tested switching Windows users accounts as that is simply far too much to ask of the end user and should not be necessary.)
Now, I should say that I have set the application to use these permissions:
string[] permissions = new string[] { "user_photos", "publish_stream", "offline_access" };
So, "offline_access" is included there. I do not know if this does/should affect logging out or not.
Again, my purpose for logging out is merely to switch users. (If there's a better approach, please let me know.)
The purported solutions seem to be:
- Use the JavaScript SDK (FB.logout())
- Use "m.facebook.com" instead
- Create your own URL (and possibly use m.facebook.com)
- Create your own URL and use the session variable (in ASP.NET)
The first is kind of silly. Why resort to JavaScript when you're using C#? It's kind of a step backwards and has a lot of additional overhead in a desktop application. (I have not tried this as it's simply disgustingly messy to do this in a desktop application.) If anyone can confirm that this is the only working method, please do so. I'm desperately trying to avoid it.
The second doesn't work. Perhaps it worked in the past, but my umpteen attempts to get it to work have all failed.
The third doesn't work. I've tried umpteen dozen variations with zero success.
The last option there doesn't work for a desktop application because it's not ASP.NET and you don't have a session variable to work with.
The Facebook C# SDK logout also no longer works. i.e.
public FacebookLoginDialog(string appId, string[] extendedPermissions, bool logout)
{
IDictionary<string, object> loginParameters = new Dictionary<string, object>
{
{ "response_type", "token" },
{ "display", "popup" }
};
_navigateUri = FacebookOAuthClient.GetLoginUrl(appId, null, extendedPermissions, logout, loginParameters);
InitializeComponent();
}
I remember it working in the past, but it no longer works now. (Which truly puzzles me...)
It instead now directs the user to the Facebook mobile page, where the user must manually logout.
Now, I could do browser automation to automatically click the logout link for the user, however, this is prone to breaking if Facebook updates the mobile UI. It is also messy, and possibly a worse solution than trying to use the JavaScript SDK FB.logout() method (though not by much).
I have searched for some kind of documentation, however, I cannot find anything in the Facebook developer documentation that illustrates how to logout an application.
Has anyone solved this problem, or seen any documentation that can be ported to work with the Facebook C# SDK?
I am certainly open to using a WebClient or HttpClient/Response if anyone can point to some documentation that could work with it. I simply have not been able to find any low-level documentation that shows how this approach could work.
Thank you in advance for any advice, pointers, or links.
回答1:
This question has been answered here, the suggestion is to use the url below to logout since facebook is apparently skipping the "next" parameter
https://www.facebook.com/logout.php?next=[redirect_uri]&access_token=[access_token]
回答2:
I'm guessing you want to log the user out so you can log another account in. In which case I would suggest this approach using the WebAuthenticationBroker:
After quite a while experimenting and having no luck with any of the examples above I have worked out that submitting a logout request with the redirect url to your login url works perfectly.
Here's the code I used to chain the requests together:
var logIn = this.Client.GetLoginUrl(
new
{
client_id = AppId,
redirect_uri = App.FacebookAuthUri.AbsoluteUri,
scope = AuthTokens.FACEBOOK_PERMISSIONS,
display = "popup",
response_type = "token"
});
var logOut = this.Client.GetLogoutUrl(new
{
next = logIn,
access_token = Settings.FacebookToken.Value
});
if (Settings.FacebookToken.Value != null) WebAuthenticationBroker.AuthenticateAndContinue(logOut);
Now every time you direct the user to the WebAuthenticationBroker the user will be redirected to the sign in page.
回答3:
See this blog post
Get the logout url using FacebookOAuthClient() & send a http request to the url..
回答4:
Well, I've turned up nothing that works, so I've resorted to a nasty hack.
The FacebookOAuthClient.GetLogoutUrl() method URL does not log me out, however, it does return an "m.facebook.com" URL, e.g.:
http://m.facebook.com/logout.php?next=http://www.facebook.com/dialog/oauth/?response_type=token&display=popup&scope=user_about_me%252coffline_access&client_id=123456789012345&redirect_uri=http%253a%252f%252fwww.facebook.com%252fconnect%252flogin_success.html&confirm=1
The mobile page has a "Logout" link at the bottom of the page.
Trying to catch the anchor tag:
HtmlElementCollection hec = wbrFacebookAuth.Document.GetElementsByTagName("a");
foreach (HtmlElement elem in hec)
{
// the logout link has a data-sigil="logout" attribute:
string datasigil = elem.GetAttribute("data-sigil").ToLower();
if (datasigil == "logout")
{
wbrFacebookAuth.Navigate(elem.GetAttribute("href"));
break;
}
}
In the Navigated WebBrowser event is unpredictable and unreliable. The actual method of catching it isn't relevant. e.g. This does not work either:
string logoutPattern = @"<a href=""(/logout.[^""]+)""";
Regex rx = new Regex(logoutPattern);
if (rx.IsMatch(wbMain.DocumentText))
{
MatchCollection mc = rx.Matches(wbMain.DocumentText);
if (mc.Count > 0)
{
foreach (Match m in mc)
{
Console.WriteLine("*** " + m.ToString());
}
}
}
However, it can be caught reliably in the DocumentCompleted event handler.
private void wbrFacebookAuth_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (_logout)
{
HtmlElementCollection hec = wbrFacebookAuth.Document.GetElementsByTagName("a");
foreach (HtmlElement elem in hec)
{
// the logout link has a data-sigil="logout" attribute:
string datasigil = elem.GetAttribute("data-sigil").ToLower();
if (datasigil == "logout")
{
wbrFacebookAuth.Navigate(elem.GetAttribute("href"));
break;
}
}
}
}
The problem is that it is now loading 2 pages instead of 1, and it's still a bit messy. However, it works.
回答5:
had the same problem with missing log out button and found via google this hint the other day:
open https://developers.facebook.com/?ref=pf (called "facebook developers site") and there you can find the usual log in/out button again.
dont ask me how it works and why it works, I only followed the instructions I found myself and it was working for me
good luck, best wishes
来源:https://stackoverflow.com/questions/6394223/cannot-logout-of-facebook-with-facebook-c-sharp-sdk