ping / instagram_private_api

A Python library to access Instagram's private API.
MIT License
2.94k stars 608 forks source link

JSONDecodeError: Expecting value: line 1 column 1 (char 0) #373

Open mikolajjesion opened 2 years ago

mikolajjesion commented 2 years ago

Which client are you using?


Describe the Bug/Error:

JSONDecodeError using web_api and tag_feed function. Does anyone know how to solve problem with below error ? "JSONDecodeError: Expecting value: line 1 column 1 (char 0)". I have followed other steps to create proper connection to web api via MyClient class, and connection is set propertly, but after calling tag_feed function I can't get any posts. Code to reproduce this error is attached below.

Code to replicate the error must be provided below.


Paste the output of python -V here:

Code:

from instagram_web_api import Client, ClientCompatPatch, ClientError, ClientLoginError
import string
import random
import hashlib

class MyClient(Client):

    @staticmethod
    def _extract_rhx_gis(html):
        options = string.ascii_lowercase + string.digits
        text = ''.join([random.choice(options) for _ in range(8)])
        return hashlib.md5(text.encode()).hexdigest()

    def login(self):
        self.username = 'username'
        self.password = 'password'
        """Login to the web site."""
        if not self.username or not self.password:
            raise ClientError('username/password is blank')

        #time = str(int(datetime.datetime.now().timestamp()))
        #enc_password = f"#PWD_INSTAGRAM_BROWSER:0:{time}:{self.password}"
        enc_password = ("#PWD_INSTAGRAM_BROWSER:0:{}:{}".format(int(time.time()), self.password)) #python2.7

        params = {'username': self.username, 'enc_password': enc_password, 'queryParams': '{}', 'optIntoOneTap': False}
        self._init_rollout_hash()
        login_res = self._make_request('https://www.instagram.com/accounts/login/ajax/', params=params)
        if not login_res.get('status', '') == 'ok' or not login_res.get ('authenticated'):
            raise ClientLoginError('Unable to login')

        if self.on_login:
            on_login_callback = self.on_login
            on_login_callback(self)
        return login_res

web_api = MyClient(auto_patch=True, drop_incompat_keys=False)
tagfeed = web_api.tag_feed('bmw')

Error/Debug Log:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-29-3df001ece85b> in <module>
----> 1 tagfeed = web_api.tag_feed(SEARCH)
      2 status = tagfeed['status']
      3 page_info = tagfeed['data']['hashtag']['edge_hashtag_to_media']['page_info']
      4 edges = tagfeed['data']['hashtag']['edge_hashtag_to_media']['edges']
      5 

G:\MJE\venvs\socialmediaEnv\lib\site-packages\instagram_web_api\client.py in tag_feed(self, tag, **kwargs)
    964         }
    965 
--> 966         return self._make_request(self.GRAPHQL_API_URL, query=query)
    967 
    968     def location_feed(self, location_id, **kwargs):

G:\MJE\venvs\socialmediaEnv\lib\site-packages\instagram_web_api\client.py in _make_request(self, url, params, headers, query, return_response, get_method)
    282             response_content = self._read_response(res)
    283             self.logger.debug('RES BODY: {0!s}'.format(response_content))
--> 284             return json.loads(response_content)
    285 
    286         except compat_urllib_error.HTTPError as e:

g:\python38\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

g:\python38\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

g:\python38\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Psycoguana commented 2 years ago

I'm experiencing the same error, did you find a solution?

Lindt19 commented 2 years ago

Same here

mikolajjesion commented 2 years ago

No. I still struggle with this error.

jastiso commented 2 years ago

I was having this issue with tag_feed but got it to work if I signed in. I think instagram returns queries in html instead of json if you aren't logged in. I achieved this by adding authenticate, user, and password arguments to MyClient

api = MyClient(auto_patch=True, authenticate=True, username=user, password=password)