views:

94

answers:

3

Hello, I'm trying to use Rohit Agarwal's BrowserSession class together with HtmlAgilityPack to login to and subsequently navigate around Facebook.

I've previously managed doing the same by writing my own HttpWebRequest's. However, it then only works when I manually fetch the cookie from my browser and insert a fresh cookie-string to the request each time I'm doing a new "session". Now I'm trying to use BrowserSession to get smarter navigation.

Here's the current code:

BrowserSession b = new BrowserSession();

b.Get(@"http://www.facebook.com/login.php");
b.FormElements["email"] = "[email protected]";
b.FormElements["pass"] = "xxxxxxxx";
b.FormElements["lsd"] = "qDhIH";
b.FormElements["trynum"] = "1";
b.FormElements["persistent_inputcheckbox"] = "1";

var response = b.Post(@"https://login.facebook.com/login.php?login_attempt=1");

The above works fine. Trouble comes when I try to use this BrowserSession again to fetch another page. I'm doing it this way since BrowserSession saves the cookies from the last response and inserts them into the next request, thus I should not have to manually inser cookiedata fetched from my browser anymore.

However, when I try to do something like this:

var profilePage = b.Get(@"https://m.facebook.com/profile.php?id=1111111111");

the doc I get back is empty. I would appreciate any input on what I'm doing wrong.

A: 

Have you checked out their new API? http://developers.facebook.com/docs/authentication/

You can call a straightforward URL to get an oauth2.0 access token and attach that on the rest of your requests...

https://graph.facebook.com/oauth/authorize?
    client_id=...&
    redirect_uri=http://www.example.com/oauth_redirect

Change redirect_uri to whatever URL you want, and it will get called back with a parameter called "access_token" on it. Get that and make whatever automated SDK calls you want.

Thanks for the answer. I'm only doing a private research in social graphing and only need to automate my own browsing through my own friends, instead of manually saving pages. It would probably take much less time doing it manually, but it'll be more fun automating :) I don't need or want and actual facebook app. Also, the API can't see all that I can see myself as a logged in user, and anyhow my current task I'm stuck at is learning how to use BrowserSession properly.
Karsa Olong
+1  A: 

Sorry, I don't know much about the HTML agility pack or BrowserSession class you've mentioned. But I did try the same scenario with HtmlUnit and it working just fine. I'm using a .NET wrapper (the source code of which can be found here and is explained a bit more here), and here's the code I've used (some details removed to protect the innocent):

var driver = new HtmlUnitDriver(true);
driver.Url = @"http://www.facebook.com/login.php";

var email = driver.FindElement(By.Name("email"));
email.SendKeys("[email protected]");

var pass = driver.FindElement(By.Name("pass"));
pass.SendKeys("xxxxxxxx");

var inputs = driver.FindElements(By.TagName("input"));
var loginButton = (from input in inputs
                   where input.GetAttribute("value").ToLower() == "login"
                   && input.GetAttribute("type").ToLower() == "submit"
                   select input).First();
loginButton.Click();

driver.Url = @"https://m.facebook.com/profile.php?id=1111111111";
Assert.That(driver.Title, Is.StringContaining("Title of page goes here"));

Hope this helps.

ShaderOp
Thank you! This was a good solution and worked just fine :)
Karsa Olong
You're most welcome. Good luck with your project :)
ShaderOp
+1  A: 

You might want to use WatiN (Web Application Testing In .Net) http://watin.sourceforge.net/

OR

Have a look at my StackOverflowClient in which I'm using HtmlAgilityPack with WebBrowser control http://stackoverflowclient.codeplex.com

You can read the following blog post on how I did it

You can obviously adapt it to use it against Facebook.

Hasan Khan
Thanks for the answer, I have found my solution for now but may come back to your examples later :)
Karsa Olong