How to configure the python Twill/Mechanize library to acces Facebook

1k Views Asked by At

I'm trying to run some automated functional tests using python and Twill. The tests verify that my application's OAuth login and connection endpoints work properly.

Luckily Twitter doesn't mind that Twill/Mechanize is accessing twitter.com. However, Facebook does not like the fact that I'm using Twill to access facebook.com. I get their 'Incompatible Browser' response. I simply want to access their OAuth dialog page and either allow or deny the application I'm testing. Is there a way to configure Twill/Mechanize so that Facebook will think its a standard browser?

2

There are 2 best solutions below

3
starenka On

Try to send user agent header w/ mechanize.

0
Douglas Adam Smith II On

Try this!

from twill.commands import *

def login(email, password):
    go('http://www.facebook.com')
    add_extra_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0")
    formclear('1')
    fv("1", "email", email)
    fv("1", "pass", password)
    fv("1", "persistent", "1")
    submit()

and then:

login('[email protected]', 'password1')