Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't login to Instagram using requests

I'm trying to login to Instagram using requests library. I succeeded using following script, however it doesn't work anymore. The password field becomes encrypted (checked the dev tools while logging in manually).

I've tried :

import re
import requests
from bs4 import BeautifulSoup

link = 'https://www.instagram.com/accounts/login/'
login_url = 'https://www.instagram.com/accounts/login/ajax/'

payload = {
    'username': 'someusername',
    'password': 'somepassword',
    'enc_password': '',
    'queryParams': {},
    'optIntoOneTap': 'false'
}

with requests.Session() as s:
    r = s.get(link)
    csrf = re.findall(r"csrf_token\":\"(.*?)\"",r.text)[0]
    r = s.post(login_url,data=payload,headers={
        "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
        "referer": "https://www.instagram.com/accounts/login/",
        "x-csrftoken":csrf
    })
    print(r.status_code)
    print(r.url)

I found using dev tools:

username: someusername
enc_password: #PWD_INSTAGRAM_BROWSER:10:1592421027:ARpQAAm7pp/etjy2dMjVtPRdJFRPu8FAGILBRyupINxLckJ3QO0u0RLmU5NaONYK2G0jQt+78BBDBxR9nrUsufbZgR02YvR8BLcHS4uN8Gu88O2Z2mQU9AH3C0Z2NpDPpS22uqUYhxDKcYS5cA==
queryParams: {"oneTapUsers":"[\"36990119985\"]"}
optIntoOneTap: false

How can I login to Instagram using requests?

like image 502
MITHU Avatar asked Jun 17 '20 19:06

MITHU


People also ask

Can't log into Instagram there was a problem with your request?

“Sorry there was a problem with your request,” which prevents you from accessing your Instagram account, is caused by a number of factors, including a suspended account for breaking Instagram's terms of service, server connectivity issues, your IP is blocked by Instagram, an incorrect username or password combination, ...

Why is my Instagram not allowing me to log into my account?

Change your password or send yourself a password reset email. Turn on two-factor authentication for additional security. Confirm your phone number and email address in account settings are correct. Check Accounts Center and remove any linked accounts you don't recognize.

How do you fix sorry there was a problem with your request on Instagram 2021?

Most of time , when there is some issue with app, clearing the app cache will resolve the issue. So, To fix “Sorry There Was a Problem With Your Request” issue, clear Instagram app cache. To clear the Instagram App cache, For Android users, Go to settings >> Find Instagram App >> Tap on clear cache.

What does it mean when Instagram says error sorry there was a problem with your request?

The 'sorry there was a problem with your request' error on Instagram occurs when your IP address is blocked by Instagram or server connectivity issues, according to Wealth Quint. As several users have noted, they are facing a login issue while trying to reactivate their accounts after a brief period of deactivation.


1 Answers

You can use authentication version 0 - plain password, no encryption:

import re
import requests
from bs4 import BeautifulSoup

from datetime import datetime

link = 'https://www.instagram.com/accounts/login/'
login_url = 'https://www.instagram.com/accounts/login/ajax/'

time = int(datetime.now().timestamp())

payload = {
    'username': '<USERNAME HERE>',
    'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{time}:<PLAIN PASSWORD HERE>',  # <-- note the '0' - that means we want to use plain passwords
    'queryParams': {},
    'optIntoOneTap': 'false'
}

with requests.Session() as s:
    r = s.get(link)
    csrf = re.findall(r"csrf_token\":\"(.*?)\"",r.text)[0]
    r = s.post(login_url,data=payload,headers={
        "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
        "referer": "https://www.instagram.com/accounts/login/",
        "x-csrftoken":csrf
    })
    print(r.status_code)
    print(r.url)
    print(r.text)

Prints:

200
https://www.instagram.com/accounts/login/ajax/
{"authenticated": true, "user": true, "userId": "XXXXXXXX", "oneTapPrompt": true, "reactivated": true, "status": "ok"}
like image 157
Andrej Kesely Avatar answered Sep 19 '22 07:09

Andrej Kesely