instaloader / instaloader

Download pictures (or videos) along with their captions and other metadata from Instagram.

Home Page:https://instaloader.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ConnectionException: JSON Query to graphql/query: HTTP error code 401.

qjvk2880 opened this issue · comments

(I'm very Sorry if this problem is already discussed and solved. I read issues for 3 hours but couldn't find proper solution)

I'm making an python program that can download reels.
it's running on my AWS EC2.
I'm using csrf token and session id for authentication.
from a few days ago, downloading doesn't work.

Received b'{"uuid":"730f196b-cc5b-4451-83fe-036a220b6cfe","url":"https://www.instagram.com/reel/C4crdNqvT7f/?utm_source=ig_web_copy_link"}'
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/instaloader/instaloadercontext.py", line 409, in get_json
raise ConnectionException("HTTP error code {}.".format(resp.status_code))
instaloader.exceptions.ConnectionException: HTTP error code 401.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/instaloader/instaloadercontext.py", line 409, in get_json
raise ConnectionException("HTTP error code {}.".format(resp.status_code))
instaloader.exceptions.ConnectionException: HTTP error code 401.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/instaloader/instaloadercontext.py", line 409, in get_json
raise ConnectionException("HTTP error code {}.".format(resp.status_code))
instaloader.exceptions.ConnectionException: HTTP error code 401.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/code/main.py", line 4, in
consumer.main()
File "/code/utils/videoUrlListener.py", line 40, in main
chan.start_consuming()
File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 1883, in start_consuming
self._process_data_events(time_limit=None)
File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 2044, in _process_data_events
self.connection.process_data_events(time_limit=time_limit)
File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 851, in process_data_events
self._dispatch_channel_events()
File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 567, in _dispatch_channel_events
impl_channel._get_cookie()._dispatch_events()
File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 1510, in _dispatch_events
consumer_info.on_message_callback(self, evt.method,
File "/code/utils/videoUrlListener.py", line 26, in on_message
publisher.send_summary(body_json['url'], body_json['uuid'])
File "/code/utils/videoSummarySender.py", line 29, in send_summary
video_info = insta.download_reels_as_audio(url, uuid)
File "/code/utils/instagram_util.py", line 20, in download_reels_as_audio
post = instaloader.Post.from_shortcode(L.context, extract_shortcode(reels_url))
File "/usr/local/lib/python3.10/site-packages/instaloader/structures.py", line 135, in from_shortcode
post._node = post._full_metadata
File "/usr/local/lib/python3.10/site-packages/instaloader/structures.py", line 252, in _full_metadata
self._obtain_metadata()
File "/usr/local/lib/python3.10/site-packages/instaloader/structures.py", line 239, in _obtain_metadata
pic_json = self._context.graphql_query(
File "/usr/local/lib/python3.10/site-packages/instaloader/instaloadercontext.py", line 470, in graphql_query
resp_json = self.get_json('graphql/query',
File "/usr/local/lib/python3.10/site-packages/instaloader/instaloadercontext.py", line 435, in get_json
return self.get_json(path=path, params=params, host=host, session=sess, _attempt=_attempt + 1,
File "/usr/local/lib/python3.10/site-packages/instaloader/instaloadercontext.py", line 435, in get_json
return self.get_json(path=path, params=params, host=host, session=sess, _attempt=_attempt + 1,
File "/usr/local/lib/python3.10/site-packages/instaloader/instaloadercontext.py", line 425, in get_json
raise ConnectionException(error_string) from err
instaloader.exceptions.ConnectionException: JSON Query to graphql/query: HTTP error code 401.

I thought that I had been banned by instagram.
but In my local desktop, downloading works using same sessionId and csrf token.

so I changed my ec2 machine ip but not solved the problem.
what is problem and what should i do?

this is my code
def download_reels_as_audio(reels_url, video_code):
L = instaloader.Instaloader(compress_json=False,
download_pictures=False,
download_comments=False,
download_video_thumbnails=False,
download_geotags=False,
max_connection_attempts=1
)
L.load_session("my_account_name",
{"sessionid": settings.INSTAGRAM["SESSION_ID"],
"csrftoken": settings.INSTAGRAM["CSRF_TOKEN"]})

post = instaloader.Post.from_shortcode(L.context, extract_shortcode(reels_url))

I'm using instaloader version 4.10.3