Skip to content

Instantly share code, notes, and snippets.

@jslim89
Created April 26, 2018 06:34
Show Gist options
  • Save jslim89/64ea8f7a24570677f06a3c8e6fc2c885 to your computer and use it in GitHub Desktop.
Save jslim89/64ea8f7a24570677f06a3c8e6fc2c885 to your computer and use it in GitHub Desktop.
Instagram login through requests
import requests
import pprint
INSTAGRAM_URL = 'https://www.instagram.com'
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
IG_ACCOUNTS = [
{'username': 'username1', 'password': 'pass1'},
{'username': 'username2', 'password': 'pass2'},
]
def login(username, password):
login_url = INSTAGRAM_URL + '/accounts/login/ajax/'
session = requests.Session()
session.headers = { 'user-agent': USER_AGENT }
session.headers.update({'Referer': INSTAGRAM_URL})
req = session.get(INSTAGRAM_URL)
session.headers.update({'X-CSRFToken': req.cookies['csrftoken']})
login = session.post(login_url, data={'username': username, 'password': password}, allow_redirects=True)
print('login: ')
pprint.pprint(login)
session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
cookies = login.cookies
results = login.json()
print('login results')
pprint.pprint(results)
if (results['authenticated'] == False):
return False, None
return True, cookies
login(IG_ACCOUNTS[0]['username'], IG_ACCOUNTS[0]['password'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment