how to login instagram programmatically

Since Instagram uses our privacy to make money, we’d better create some bots to fuck this bitch. To write an instagram bot, the first issue we’ll encounter is how to login instagram programmatically. The login process has two steps.

The first step is to visit the url https://i.instagram.com/api/v1/si/fetch_headers/?challenge_type=signup&guid=[uuid] to get a csrftoken. csrftoken is returned as a cookie. In python, we can get the value of the cookie as:

cookies=session.cookies.get_dict()
csrftoken=cookies['csrftoken']

Next step is to post the username/password,etc., together with the csrftoken to the following url:https://i.instagram.com/api/v1/si/fetch_headers/accounts/login/

url="https://i.instagram.com/api/v1/si/fetch_headers/accounts/login/"
postdata = json.dumps({
                    'phone_id': str(uuid.uuid4()),
                    '_csrftoken': csrftoken,
                    'username': username,
                    'guid': str(uuid.uuid4()),
                    'device_id': str(uuid.uuid4()),
                    'password': password,
                    'login_attempt_count': '0',
                })
response = session.post(url, data=postdata)

Now wet get the login cookie in the session and complete the login process. We can save the cookie in a file for later use. The uniqueness of this auto-login program is it must have the csrftoken before the real login so it requires a two-step login. Another difference than other auto-bots is the post data format. The post data must be a JSON string and contain some extra fields besides user name and password.

Leave a Reply