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

Why I getting CSRF token missing message in login? #1216

Open parsasaei opened 1 year ago

parsasaei commented 1 year ago

Hi, I wrote this code for logging in:


class InstagramDownloader(object):
        def __init__(self):
            self.client = self.login()
        def login(self):
                self.SESSION_FILE = "./dump.json"
                self.CHALLENGE_EMAIL = "@gmail.com"
                self.CHALLENGE_PASSWORD = "ddddssss"
                self.IG_USERNAME = "__________"
                self.IG_PASSWORD = "__________"
                SETTINGS = {
                    "uuids": {
                        "phone_id": "f6b53479-78df-46fb-9ff0-62f76e0cf5df",
                        "uuid": "6f12cb7e-8e3b-48b3-b26c-0e0d080739fa",
                                "client_session_id": "2db1a31c-e18e-4c43-9719-64b69649d3a1",
                                "advertising_id": "0bebc267-79e3-4482-88df-71acf3832153",
                                "device_id": "android-d87de5d6de13d442"
                    },
                    "cookies":  {},  # set here your saved cookies
                    "last_login": TEN_MINUTES_AGO_SEC_INT,
                    "device_settings": {
                        "cpu": "Qualcomm Technologies, Inc SDM636",
                        "dpi": "434dpi",
                        "model": "Nokia 6.1 Plus",
                        "device": "DRG_sprout",
                        "resolution": "1080x2280",
                        "app_version": "2021.08.01",
                        "manufacturer": "HMD Global",
                        "version_code": "1627776000000",
                        "android_release": "10",
                        "android_version": 29
                    },
                    "user_agent": "Mozilla/5.0 (Linux; Android 10; Nokia 6.1 Plus) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.104 Mobile Safari/537.36"
                }

                cl = Client(SETTINGS)

                cl.handle_exception = self.handle_exception

                if (os.path.exists(self.SESSION_FILE)):
                    cl.load_settings(self.SESSION_FILE)
                    cl.login(self.IG_USERNAME, self.IG_PASSWORD)
                    cl.get_timeline_feed()
                else:
                    cl.login(self.IG_USERNAME, self.IG_PASSWORD)
                    cl.dump_settings(self.SESSION_FILE)
                return cl

            def handle_exception(self, client, e):
                print(e)
                pass

        if __name__ == "__main__":
            main = InstagramDownloader()
            client = main.client

But I get this exception in the exception handler function with 403 status error: {"message":"CSRF token missing or incorrect","status":"fail"}

What can I do for solve this issue?

patilaum commented 1 year ago

@parsasaei Are you using TOR here?

parsasaei commented 1 year ago

@parsasaei Are you using TOR here?

No, I'm not used TOR

MainKronos commented 1 year ago

I have the same problem when I try to login

instagrapi.exceptions.ClientForbiddenError: {"message":"CSRF token missing or incorrect","status":"fail"}
parsasaei commented 1 year ago

@MainKronos I found when we send user-agent as browser type get this error, so with some searches I found the user-agent here must be have a structure like instagram app needs like this I used: Instagram 276.1.0.26.103 Android (29/10; 434dpi; 1080x2280; qualcomm; r10p4; ssssss; ssssss; it_IT; 1627776000) This link can help you to make something like instagram app user-agent structure : https://whatmyuseragent.com/apps/instagram-app