academica academica - 3 months ago 16
Python Question

login to facebook using the Ghost.py python package

The task is login on facebook page using a webengine. I have choosen Ghost.py for this. Installed pySide, Flask and paste modules and got Ghost.py with command 'pip install Ghost.py' eventually.
My login script is simple:

from ghost import Ghost
ghost = Ghost()
session = ghost.start()
page, resources = session.open('https://www.facebook.com/login.php')

result, resources = session.set_field_value("input[id=email]", 'email')
result, resources = session.set_field_value("input[id=pass]", 'passssss')

page, resources = session.call("form", "submit", expect_loading=True)
session.capture_to('I:\\beer.png')


But when I'm using my windows PC I see "enable cookie in your browser" message on facebook page. When I use my laptop with fedora installed I logins successfully.
What is the difference and how to beat this issue?
I guess it is old webkit version on my windows PC.

Answer

As at this time, I am able to log into Facebook on my Windows PC with the code below. Any time it changes as a result of upgrade from their end, then alter the affected part. Cheers.

from ghost import Ghost, Session

ghost = Ghost()
USERAGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"

with ghost.start():
    session = Session(ghost, download_images=False, display=True, user_agent=USERAGENT)
    page, rs = session.open("https://m.facebook.com/login.php", timeout=120)
    assert page.http_status == 200

    session.evaluate("""
    document.querySelector('input[name="email"]').value = 'email@email.com';
    document.querySelector('input[name="pass"]').value = 'email-password';
    """)

    session.evaluate("""document.querySelector('input[name="login"]').click();""",
                 expect_loading=True)

    """
    import codecs

    with codecs.open('fb.html', encoding='utf-8', mode='w') as f:
       f.write(session.content)
    """

    # session.save_cookies('fbookie')
    session.capture_to(path='fbookie.png')

    # gracefully clean off to avoid errors
    session.webview.setHtml('')
    session.exit()
Comments