Skip to content

Instantly share code, notes, and snippets.

@UndergroundLabs
Created October 3, 2015 19:46
Show Gist options
  • Save UndergroundLabs/fad38205068ffb904685 to your computer and use it in GitHub Desktop.
Save UndergroundLabs/fad38205068ffb904685 to your computer and use it in GitHub Desktop.
Facebook Python Login Script
#!/home/drspock/scripts/FBInvite/bin/python
import argparse
import requests
import pyquery
def login(session, email, password):
'''
Attempt to login to Facebook. Returns user ID, xs token and
fb_dtsg token. All 3 are required to make requests to
Facebook endpoints as a logged in user. Returns False if
login failed.
'''
# Navigate to Facebook's homepage to load Facebook's cookies.
response = session.get('https://m.facebook.com')
# Attempt to login to Facebook
response = session.post('https://m.facebook.com/login.php', data={
'email': email,
'pass': password
}, allow_redirects=False)
# If c_user cookie is present, login was successful
if 'c_user' in response.cookies:
# Make a request to homepage to get fb_dtsg token
homepage_resp = session.get('https://m.facebook.com/home.php')
dom = pyquery.PyQuery(homepage_resp.text.encode('utf8'))
fb_dtsg = dom('input[name="fb_dtsg"]').val()
return fb_dtsg, response.cookies['c_user'], response.cookies['xs']
else:
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Login to Facebook')
parser.add_argument('email', help='Email address')
parser.add_argument('password', help='Login password')
args = parser.parse_args()
session = requests.session()
session.headers.update({
'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:39.0) Gecko/20100101 Firefox/39.0'
})
fb_dtsg, user_id, xs = login(session, args.email, args.password)
if user_id:
print '{0}:{1}:{2}'.format(fb_dtsg, user_id, xs)
else:
print 'Login Failed'
@Plawn
Copy link

Plawn commented May 25, 2018

login should always return a tuple with a length of 3

@ledderio
Copy link

ledderio commented May 1, 2019

Working 2 april 2019. Thanks!

@Sureya
Copy link

Sureya commented Sep 5, 2019

September 5 2019, Working. Thanks

@Bouzazi
Copy link

Bouzazi commented Sep 25, 2019

September 25, 2019, Working!

@otaku1
Copy link

otaku1 commented Dec 1, 2019

i am facing facebook checkpoint.....

fb

@Leesinbaka
Copy link

December 4, 2019 Working! Thanks

@trieunvhq
Copy link

Please help me? Where is the session taken? How to get a session?

@yozshujar
Copy link

Dec 17, 2020 Working! Thx

@Riadhcoding
Copy link

i am facing facebook checkpoint.....

fb

hey bro type in google my user agent and copy ur user in >>> 'User-Agent':

@pf5179vr
Copy link

pf5179vr commented Jan 6, 2021

Jan 6 2021, Working. Thanks bro

@kanxck
Copy link

kanxck commented Jan 6, 2021

How run

@numan98khan
Copy link

doesn't work anymore

@shoukreytom
Copy link

doesn't work anymore

you have to compile it with python2 or try this python3

import argparse
import requests
import pyquery

def login(session, email, password):
    
    '''
    Attempt to login to Facebook. Returns user ID, xs token and
    fb_dtsg token. All 3 are required to make requests to
    Facebook endpoints as a logged in user. Returns False if
    login failed.
    '''

    # Navigate to Facebook's homepage to load Facebook's cookies.
    response = session.get('https://m.facebook.com')
    
    # Attempt to login to Facebook
    response = session.post('https://m.facebook.com/login.php', data={
        'email': email,
        'pass': password
    }, allow_redirects=False)
    
    # If c_user cookie is present, login was successful
    if 'c_user' in response.cookies:

        # Make a request to homepage to get fb_dtsg token
        homepage_resp = session.get('https://m.facebook.com/home.php')
        
        dom = pyquery.PyQuery(homepage_resp.text.encode('utf8'))
        fb_dtsg = dom('input[name="fb_dtsg"]').val()

        return fb_dtsg, response.cookies['c_user'], response.cookies['xs']
    else:
        return False 

if __name__ == "__main__":
    
     parser = argparse.ArgumentParser(description='Login to Facebook')
     parser.add_argument('email', help='Email address')
     parser.add_argument('password', help='Login password')

     args = parser.parse_args()

    session = requests.session()
    session.headers.update({
        'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:39.0) Gecko/20100101 Firefox/39.0'
    })

    fb_dtsg, user_id, xs = login(session, args.email, args.password)
    
    if user_id:
        print('{0}:{1}:{2}'.format(fb_dtsg, user_id, xs))
    else:
        print ('Login Failed')

@jaesm14774
Copy link

Can anyone work now?(2021/07/31)

I can not login successfully anymore

@NoobDecompiler
Copy link

Not Working In 28 Sep 2021

@Leesinbaka
Copy link

Facebook change to session policy : O so is normal not working

@alek-tech
Copy link

Can anyone specifiy why is it not working? Has facebook changed the required data we should pass with a request to login? And if so
can someone figure out what is the new data?

@UndergroundLabs
Copy link
Author

I will fix and repost over the weekend. I've been away for a very long time.

@alek-tech
Copy link

Thanks man, and welcome back.

@renelc30
Copy link

renelc30 commented Jul 4, 2022

is FB login doable with the facebook session policy? @UndergroundLabs ? if so I can try to make it work

@on
Copy link

on commented May 28, 2023

is FB login doable with the facebook session policy? @UndergroundLabs ? if so I can try to make it work

it definitely is

@emanalqudah
Copy link

error: the following arguments are required: email, password
i am trying to put username and password but it gives error

@emanalqudah
Copy link

Working 2 april 2019. Thanks!

it doesn't work!!!!how it works with you ,please.??

@Riadhcoding
Copy link

Riadhcoding commented Nov 24, 2023 via email

@T31337
Copy link

T31337 commented Mar 18, 2024

File "C:\Users\Admin\Dev\Python\PyProject\fb-login.py", line 57, in
fb_dtsg, user_id, xs = login(session, args.email, args.password)
^^^^^^^^^^^^^^^^^^^^
TypeError: cannot unpack non-iterable bool object

=====================================================
This Might Be A Parsing Error Due To {and} in user password?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment