Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

Instagram login with python

I am writing a python function that can log into instagram using requests, I have been able to get the csrf token and add it to my header, but I am not stuck trying to make the right request to login. I have tried two headers and they both return 'Bad request'. Any help would be appreciated.

This is the header I tried to make to login

header = {
        'user-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
        'Accept-Encoding': 'gzip, deflate, br',
        'cookie': f'ig_did={ig_did}; csrftoken={csrf}; mid={mid}; ig_nrcb={ig_nrcb}',
        'X-Requested-With': 'XMLHttpRequest',
        'Referer': 'https://www.instagram.com/accounts/login',
        'x-csrftoken': csrf
    }

This is the header I copied from chrome dev tools when I couldn't figure out mine

header0 = {
    'authority': 'www.instagram.com',
    'method': 'POST',
    'path': '/accounts/login/ajax/',
    'scheme': 'https',
    'accept': '*/*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9',
    'content-length': '273',
    'content-type': 'application/x-www-form-urlencoded',
    'cookie': f'ig_did={ig_did}; csrftoken={csrf}; mid={mid}; ig_nrcb={ig_nrcb}',
    'origin': 'https://www.instagram.com',
    'referer': 'https://www.instagram.com/',
    'sec-ch-ua': 'Google Chrome;v=87, Not;A Brand;v=99, Chromium;v=87',
    'sec-ch-ua-mobile': '?0',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
    'x-csrftoken': csrf,
    'x-ig-app-id': '936619743392459',
    'x-ig-www-claim': '0',
    'x-instagram-ajax': 'b10813bd9030',
    'x-requested-with': 'XMLHttpRequest'
    }

This is the data im sending (I am aware the password probably isnt correct)

payload = {
        'username': username,
        'password': f'#PWD_INSTAGRAM_BROWSER:10:{time}:{password}',
        'queryParams': {},
        'optIntoOneTap': 'false'
    }

This is the request im sending

loginResponse = requests.post(loginUrl, data = payload, headers = header)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...