subzeroid / instagrapi

🔥 The fastest and powerful Python library for Instagram Private API 2024
https://hikerapi.com/p/bkXQlaVe
MIT License
4.38k stars 685 forks source link

[BUG] Login required after every 2 / 3 days #969

Open RoyalBosS-Ayush opened 2 years ago

RoyalBosS-Ayush commented 2 years ago

Describe the bug I do a new login. Save settings in file. Next time I load settings and login through that (no new session created). After Two or Three days, Instagram otp challenge appears. I use cl.media_pk_from_url cl.media_info cl.story_pk_from_url cl.story_info cl.cl.user_info_by_username with time gap (max 50 requests in a day)

To Reproduce

cl = Client()
cl.login(username, password)
settings = cl.get_settings()
save_settings(settings) # save settings in file
from instagrapi import Client
settings = load_settings() # return saved settings in file
cl = Client(settings)
cl.login(username, password) # no new session created.

Expected behavior login_required error

Desktop (please complete the following information):

Additional context I am asked to login by solving otp challenge (mostly sms otp) after 2 or 3 days from the day i created session.

vttrz commented 1 year ago

Hello @RoyalBosS-Ayush, I had the same problem, so I created a logic to check if the login is ok.

from os.path import exists

  @staticmethod
  def __get_client() -> Client:
      client = Client()

      if exists('./tmp/dump.json'):

          loaded_settings = client.load_settings('./tmp/dump.json')

          # validate if client is logged
          if not loaded_settings.get('last_login'):
              remove('./tmp/dump.json')

              # recursive call
              return UserService.__get_client()
      else:
          client.login(os.environ.get("INSTAGRAM_USER"), os.environ.get("INSTAGRAM_USER_PASSWORD"))
          client.dump_settings('./tmp/dump.json')

      return client
RoyalBosS-Ayush commented 1 year ago

How does it solve the issue? Instagram will still log me out and i'll have to solve challenge to continue using id.