我正在构建一个使用登录界面的python应用程序,通过直接传递用户登录数据自动登录到facebook。主要目标是从facebook获取access_token。
facebook登录skript看起来像这样:
import mechanize
br = mechanize.Browser()
br.set_handle_equiv( True )
br.set_handle_gzip( True )
br.set_handle_redirect( True )
br.set_handle_referer( True )
br.set_handle_robots( False )
br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1)
br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1' ) ]
user = 'me@mymail.com'
password = 'myPass'
# note that client_id is pseudo-nr
url = 'https://m.facebook.com/dialog/oauth?client_id=123456789012345&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token'
#Open URL and submit
br.open(url)
br.select_form(nr=0)
br.form['email'] = user
br.form['pass'] = password
br.submit()
result = br.geturl()
print result
实际上脚本工作得很好,但是在
之后似乎存在重定向问题br.submit()
通过Firefox登录到指定的URL后,手动添加用户名/密码并按下提交按钮后,将调用以下URL:
https://www.facebook.com/login.php?login_attempt=1&next=https%3A%2F%2Fwww.facebook.com%2Fv2.0%2Fdialog%2Foauth%3Fredirect_uri%3Dhttps%253A%252F%252Fwww.facebook.com%252Fconnect%252Flogin_success.html%26response_type%3Dtoken%26client_id%3D123456789012345%26ret%3Dlogin&lwv=100
这是由br.submit()命令提供的相同URL。
不幸的是,Firefox正在执行另一个重定向,首先是:
https://www.facebook.com/v2.0/dialog/oauth?redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html&response_type=token&client_id=123456789012345&ret=login&ext=143846220&hash=KefDdKCLQeJo_hJR
然后是最终目的地:
https://www.facebook.com/connect/login_success.html#access_token=CAAGXPLMhY0cYMlFbAPXJnPKtEPPQCP4NNZCyJkiRLE1B0iHeK6KonmgsyrzQu4uJbfe1TDV1YrmtyFb3SuYJAVhZBazHDr49oYh3i5WThZAfgTDmtHFsmpvn3ZBpeJ3NR718Dn9rm0PX4ZCpsBA6wAItUdHDcAiOZBIZACliuVEefqseFXtqCNFN2ZC9UGslohsZD&expires_in=3780
我不知道为什么机械化没有执行此重定向。任何想法如何解决这个问题?