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?] requests.exceptions.TooManyRedirects: Exceeded 30 redirects. #819

Closed kostyakoz closed 1 year ago

kostyakoz commented 2 years ago

Describe the bug Getting this error after multiple times of using cl.user_medias(page_id, 15) function. I'm tried with different proxies.

To Reproduce

from instagrapi import Client
...
cl = Client()
cl.login(account[0], account[1])
data = cl.user_medias(page_id, 15)

Traceback

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/public.py", line 132, in _send_public_request
    self.last_public_json = response.json()
  File "/usr/local/lib/python3.8/dist-packages/requests/models.py", line 910, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/media.py", line 677, in user_medias
    medias = self.user_medias_gql(user_id, amount)  # retry
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/media.py", line 518, in user_medias_gql
    medias_page, end_cursor = self.user_medias_paginated_gql(
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/media.py", line 467, in user_medias_paginated_gql
    data = self.public_graphql_request(
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/public.py", line 210, in public_graphql_request
    body_json = self.public_request(
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/public.py", line 76, in public_request
    raise e  # Stop retries
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/public.py", line 74, in public_request
    return self._send_public_request(url, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/public.py", line 138, in _send_public_request
    raise ClientLoginRequired(e, response=response)
instagrapi.exceptions.ClientLoginRequired: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/media.py", line 554, in user_medias_paginated_v1
    items = self.private_request(
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 447, in private_request
    raise e
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 434, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 289, in _send_private_request
    response = self.private.get(api_url, params=params)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 677, in send
    history = [resp for resp in gen]
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 677, in <listcomp>
    history = [resp for resp in gen]
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 166, in resolve_redirects
    raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

Expected behavior Getting user medias.

Desktop (please complete the following information):

Additional context I wonder why this error happens. Too many requests? Block by proxy/IP? Or problems with account (temp ban)?

comedsh commented 2 years ago

I've got the same error.

2022-07-31 09:09:51,533 INFO [4606178816] <instagrapi_crawler.py, InstagrapiPageCrawler, next_page, 0066>: crystalward2228, try to fetch the next page with (6110781932, None)
^@2022-07-31 09:10:01,812 ERROR [4606178816] <media.py, instagrapi, my_user_medias_paginated_v1, 0148>: Exceeded 30 redirects.
Traceback (most recent call last):
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/instacrawl/crawlerprovider/providers/instagrapi_override/media.py", line 137, in my_user_medias_paginated_v1
    items = self.private_request(
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/instagrapi/mixins/private.py", line 447, in private_request
    raise e
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/instagrapi/mixins/private.py", line 434, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/instagrapi/mixins/private.py", line 289, in _send_private_request
    response = self.private.get(api_url, params=params)
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/requests/sessions.py", line 600, in get
    return self.request("GET", url, **kwargs)
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/requests/sessions.py", line 723, in send
    history = [resp for resp in gen]
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/requests/sessions.py", line 723, in <listcomp>
    history = [resp for resp in gen]
  File "/Users/shangyang/.local/share/virtualenvs/instacrawl-rjFv3BNe/lib/python3.8/site-packages/requests/sessions.py", line 191, in resolve_redirects
    raise TooManyRedirects(
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.
adw0rd commented 2 years ago

@kostyakoz I cannot reproduce

image

but you can try using _v1:

data = cl.user_medias_v1(cl.user_id, 1)
kostyakoz commented 2 years ago

@adw0rd same results with _v1:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/media.py", line 554, in user_medias_paginated_v1
    items = self.private_request(
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 447, in private_request
    raise e
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 434, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 289, in _send_private_request
    response = self.private.get(api_url, params=params)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 677, in send
    history = [resp for resp in gen]
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 677, in <listcomp>
    history = [resp for resp in gen]
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 166, in resolve_redirects
    raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

It happens after multiple calls of the method. I wonder what reason cause it, API limits maybe?

adw0rd commented 2 years ago

@kostyakoz maybe yes, can you try another account/proxy for test?

kostyakoz commented 2 years ago

@adw0rd changing proxy for cached account keep this problem; fresh accounts work for some time (few hours), also I'm using sleep for at least one minute between calls. As I understand when limits reached API should rise respective error?

By the way are limits available somewhere? As I read, 150 calls per acc/day. In my situation no more than 90 calls per acc/day made, all accounts SMS verified.

adw0rd commented 2 years ago

@kostyakoz my accounts process up to ~1000 requests per day, but I have different types of requests

IDK the actual instagram limits, i think it's the values floating

kostyakoz commented 2 years ago

@adw0rd I'm also make cl.user_stories(page_id) requests, but less often. I've checked it now, and these requests don't give this error (with same accounts & proxies), only with cl.user_medias(page_id, 10)

kostyakoz commented 2 years ago

For some reason when this error raise it even can't be handled with except requests.exceptions.TooManyRedirects:. How it can be handled?

try: cl.user_medias_v1(page_id, 10)
except requests.exceptions.TooManyRedirects: 
Exceeded 30 redirects.
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/media.py", line 554, in user_medias_paginated_v1
    items = self.private_request(
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 447, in private_request
    raise e
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 434, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/instagrapi/mixins/private.py", line 289, in _send_private_request
    response = self.private.get(api_url, params=params)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 677, in send
    history = [resp for resp in gen]
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 677, in <listcomp>
    history = [resp for resp in gen]
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 166, in resolve_redirects
    raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.
olli-dot-dev commented 2 years ago

I had the same error for the last 2-3 days by now. No "job" finished successfully. I now had time to take a look at it and tried a frehs login (same account, same settings, same proxy - just a fresh login).

After entering the 6-digit 2FA code the script started running again and seems to be working.

Maybe doing a fresh login helps others, too?

adrianvignolo commented 2 years ago

@adw0rd Very nice work by the way.

Hi, Can you recommend a tutorial on how to implement the challenge function? I'm not using 2 factor auth so I hope it'll be easier to use that challenge function.