instagrapi.exceptions.UnknownError Confirmation Code

487 views Asked by At

I'm new to using instagrapi and I want to understand if there is a way to automate the confirmation code process.

from instagrapi import Client

cl = Client()
cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)

user_id = cl.user_id_from_username("adw0rd")
medias = cl.user_medias(user_id, 20)

Every time I run the code, the verification code sent to my email is required. It's not sustainable for the app I want to make.

1

There are 1 answers

0
xxredxoctoberxx On

you should not try and login each and everytime, thats not human behaivor and is suspicious to say the lease - always try to login using stored session information. here's an example from the API docs; for first login:

from instagrapi import Client

cl = Client()
cl.login(USERNAME, PASSWORD)
cl.dump_settings("session.json")

and for the next time you login:

from instagrapi import Client

cl = Client()
cl.load_settings("session.json")
cl.login (USERNAME, PASSWORD) # this doesn't actually login using username/password but uses the session
cl.get_timeline_feed() # check session

that way you can prevent instagram from asking you for a verification code each and every time. more on this and best practices: https://subzeroid.github.io/instagrapi/usage-guide/best-practices.html

Now when it comes for extracting the verification code, you can simply catch the exception 'ChallengeRequired' and handle it by using the email and imaplib libraries like so:

def get_code_from_email(username):

            mail = imaplib.IMAP4_SSL('outlook.office365.com')
            mail.login(email_user, email_password)
            mail.select("inbox")
            result, data = mail.search(None, "(UNSEEN)")
            assert result == "OK", "Error1 during get_code_from_email: %s" % result
            ids = data.pop().split()
            for num in reversed(ids):
                mail.store(num, "+FLAGS", "\\Seen")  # mark as read
                result, data = mail.fetch(num, "(RFC822)")
                assert result == "OK", "Error2 during get_code_from_email: %s" % result
                msg = email.message_from_string(data[0][1].decode())
                payloads = msg.get_payload()
                if not isinstance(payloads, list):
                    payloads = [msg]
                code = None
                for payload in payloads:
                    body = payload.get_payload(decode=True).decode()
                    if "<div" not in body:
                        continue
                    match = re.search(">([^>]*?({u})[^<]*?)<".format(u=username), body)
                    if not match:
                        continue
                    print("Match from email:", match.group(1))
                    match = re.search(r">(\d{6})<", body)
                    if not match:
                        print('Skip this email, "code" not found')
                        continue
                    code = match.group(1)
                    if code:
                        return code
            return False

this code inside the handler should do the trick. too see how to implement it inside your code please refer here: https://subzeroid.github.io/instagrapi/usage-guide/challenge_resolver.html

goodluck :)