Python Forum
Can't log in with requests post
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't log in with requests post
#1
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)
Reply
#2
Still need help guys
Reply
#3
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"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  appending variabl a URL for requests.post step duckredbeard 3 1,704 Sep-05-2020, 01:14 AM
Last Post: duckredbeard
  No route to host error when using requests.post in python on raspberry pi mariummalik22 0 4,028 Jan-06-2018, 08:34 PM
Last Post: mariummalik22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020