Python Forum

Full Version: Can't log in with requests post
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm creating an Instagram sample script but when I try to login with requests.post it doesn't seem to work. I'm able to login with post on many other websites but I can't get it to work
with a few ones(Including Instagram) Wall
### Instagram Turbo ###
import requests

wanted_username = 'justinbieber'

check = requests.get('https://www.instagram.com/' + wanted_username + '/')

print(check.url)
answer = check.ok
print(answer)

def login():
    login_url = 'https://www.instagram.com/'
    with requests.session() as c:
        c.get(login_url)
        token = c.cookies['csrftoken']
        print(token)

        payload = {
            'username': '*****',
            'password': '*****',
            'access_token': token
        }

        post = c.post(login_url, data=payload, allow_redirects=True)

        edit = c.get('https://www.instagram.com/accounts/edit/')
        print(edit.url)
login()
Here's the output i get:
Quote:https://www.instagram.com/justinbieber/
True
QDuwipIZbe5ICf2iXmztS0uCpFfrxKf3
https://www.instagram.com/accounts/login...unts/edit/

I know that the login fails because the url that is printed redirects me to the login page.
I would really appreciate some help Angel
(Please don't tell my to use the api, it's not an option in my case. Thank you)
Still need help guys
This is a year old basically, but incase you never figured out man. The CSRF token has to be in the header and you dont have headers for your request. Just a body. You also tried logging at the wrong URL. Send a post request to "www.instagram.com/accounts/login/ajax". Here's an example (i tried putting indents but this website gets rid of them). if this doesnt work for you, message me on instagram. Its the same as my username on here

InstagramTurbo = requests.session()
getCSRF = InstagramTurbo.get("https://www.instagram.com/accounts/login")
cookieString = str(getCSRF.cookies)
LoginCSRFtoken = cookieString[37:69]


data = {'username':"username",
'password':"password",
'queryParams': "{}"}


RequestHeaders = {'origin': "www.instagram.com",
'accept-encoding': "gzip, deflate, br",
'accept-language': "en-US,en;q=0.9",
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
'x-requested-with': "XMLHttpRequest",
'x-csrftoken': LoginCSRFtoken,
'x-instagram-ajax': "de81cb3fd9c4-hot",
'content-type': "application/x-www-form-urlencoded",
'accept': "*/*",
'referer': "https://www.instagram.com/accounts/login"}

login = InstagramTurbo.post("https://www.instagram.com/accounts/login/ajax/", data=RequestBody, headers=RequestHeaders)
if 'authenticated": true,' in str(login.text):
print "logged in"