Can't login with instabot

Viewed 8082

I'm having problems with instabot. I want to upload an image but it gives me an error

My code:

bot = Bot()
bot.login(username=username, password=password)
bot.upload_photo(image,caption='title')

Error:

line 23, in <module> bot.login(username=username, password=password)
line 443, in login if self.api.login(**args) is False
line 240, in login self.load_uuid_and_cookie(load_cookie=use_cookie, load_uuid=use_uuid)
line 199, in load_uuid_and_cookie return load_uuid_and_cookie(self, load_uuid=load_uuid, load_cookie=load_cookie)
line 352, in load_uuid_and_cookie
cookie_username = self.cookie_dict["ds_user"]
KeyError: 'ds_user'
7 Answers

From what I understood the part that makes the cookie does not write details about the user. so I recommend deleting the the json file in the config directory at the start of your code:

import os 
import glob
cookie_del = glob.glob("config/*cookie.json")
os.remove(cookie_del[0])

It will work but you need to find the api_login.py file of instabot.

Then comment out the 3 lines of code (352, 353, 354)

# cookie_username = self.cookie_dict["ds_user"]
# assert cookie_username == self.username.lower()
# self.cookie_dict["urlgen"]

Don't worry it won't affect the performance of the code. I have checked most of its functions and they work completely fine.

I also faced the same error and tried many things from reinstalling instabot, logging out instagram from everydevice but didn't help.

But i found some answers on web for a similar error and it worked for me.

There is a config folder created in the same directory where your python file ran, just delete that folder whenever you receive this error.

This would help.

Edit 1 : Well, i dont think the whole folder needs to be deleted,it would have many text file and one JSON file , the JSON file only needs to be deleted. it would be starting with your Insta username and some more text ahead.

So the main problem here is the JSON file. All I did is to remove the file every time it is created. What I did:

from os import remove
remove("path of JSON file")
#remaining code

Since I used igbot locally, I needed to delete the <username>_uuid_and_cookie.json file under the igbot/config directory. Made the bot login back with a non-faulty session, and it worked.

Here if you delete the config file, then the bot will login every time to fix the error go to C:\Users\{Your computer username}\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\instabot\api/api_login.py

and comment the code:

cookie_username = self.cookie_dict["ds_user_id"]
assert cookie_username == self.username.lower()
self.cookie_dict["urlgen"]

Currently I resolved with this: Remove all cookies data from the instabot to renew the login and execute the code succesfully.

cookie_del = glob.glob("config/*cookie.json")
if cookie_del : os.remove(cookie_del[0])
Related