Logging Out of Facebook in C# Without 0Auth

InPursuit

I’ve never done anything in C# before the company bought me this new laptop. The old machine I have that is running Vista won’t install Microsoft’s Visual Studio, so I haven’t been able to run it. I wasn’t aware of how much you can do with their free Express package until I decided, on a lark, to try it.

I am amazed at how easy it is to work in C# with my knowledge of C, C++ and Java. I am by no means proficient in those yet but the combination of knowledge from the different packages transfers easily. So, I’m doing what I proposed for a marketing presentation in college; I’m writing a package to automate tasks on several social media sites.

I want the program to automate tasks the way a human does them, filling out forms and clicking buttons instead of using the APIs of those respective sites. I don’t want the package to need keys for 0Auth because you are inherently restricted in the functionality you can provide by the limitations of the API and the limitations of the agreement you enter into with the sites for interfacing your software with their site.

Houston, I have a problem!

I’ve built the GUI and some basic functionality. Now I’m working on the nitty gritty, interfacing with the web pages through the web browser I plugged into the program. The difficult part of this is learning to navigate the document and interact with the web page. I was up until 7:00 a.m., researching solutions to this problem. I grabbed four hours sleep, then worked on the problem some more until I left to teach in the evening.

Houston, I’ve found some solutions!

I was sitting there with empty code on a button listener, trying to figure out how to write script to log out of Facebook. I read every article I could find, trying code only to erase it. Finally, right before dinner, I found some promising code on the MSDN site. This code had been regurgitated by other programmers but none of it seemed to work. This bit of code was more complete, so I tried it. I placed a message box strategically to tell me if the code was found on Facebook. It worked!

After dinner, I replaced the message box with code I also found on MSDN, changing it to fit my use, clicking the “Log Out” button on the Facebook logout form. Success!

This is the basic code needed to automate other interactions with forms on web pages. I’m providing the complete code for the event listener attached to the button so you can see how it works. The code searches the document for every instance of an “INPUT” element, looks for every one with a “type” of “submit” and checks each one of those for a “value” of “Log Out,” the name of the facebook button the user sees.

I have added more code that is not listed in this article to make this production ready, to make sure the document is fully loaded and to prevent errors in handling. However, the code I’ve listed here works and it takes me a huge leap forward in writing a usable program. I hope it is useful to you.

Facebook application logout using POST instead of 0Auth.

//
// This function is a button’s event listener.
private void button3_Click(object sender, EventArgs e)
{
//
// Check to see if there is a webpage (document) in the browser.
if (webBrowser1.Document != null)
{
//
// If something is there, inititalize the code to search for “INPUT” tags on the page.
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName(“INPUT”);
//
// Each time you find an “INPUT” tag, do the following …
foreach (HtmlElement elem in elems)
{
//
// typeStr will get the “type” of each “INPUT” element it encounters.
// If a type is “submit,” check further …
String typeStr = elem.GetAttribute(“type”);
if (typeStr == “submit”)
{
//
// contentStr will get the “value” listed for each “type” in the element.
// If the value is “Log Out,” click that element
String contentStr = elem.GetAttribute(“value”);
if (contentStr == “Log Out”)
{
elem.InvokeMember(“click”);
}
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s