instagram_private_api icon indicating copy to clipboard operation
instagram_private_api copied to clipboard

WebAPI: Unable to get rhx_gis from init request

Open xinhuang327 opened this issue 6 years ago • 39 comments

Please follow the guide below

  • Issues submitted without this template format will be ignored.
  • Please read the questions carefully and answer completely.
  • Do not post screenshots of error messages or code.
  • Put an x into all the boxes [ ] relevant to your issue (==> [x] no spaces).
  • Use the Preview tab to see how your issue will actually look like.
  • Issues about reverse engineering is out of scope and will be closed without response.
  • Any mention of spam-like actions or spam-related tools/libs/etc is strictly not allowed.

Before submitting an issue, make sure you have:

  • [x] Updated to the lastest version v1.6.0
  • [x] Read the README and docs
  • [x] Searched the bugtracker for similar issues including closed ones
  • [x] Reviewed the sample code in tests and examples

Which client are you using?

  • [ ] app (instagram_private_api/)
  • [x] web (instagram_web_api/)

Describe your issue

Hi everyone,

From today web api will report error "Unable to get rhx_gis from init request". I researched about this issue in other projects, some suggest to remove rhx_gis and X-Instagram-GIS, I tried this solution, at first it could successfully login and call api, but later web api reported "instagram_web_api.errors.ClientBadRequestError". I had also processed the challenges from IG website, but still no success any more.

Would be very nice if someone finds how to fix this new change.

Thanks.


Paste the output of python -V here:

Code:

# Example code that will produce the error reported
from instagram_web_api import Client

    api = Client(proxy=getProxy(),  settings=None,
                 username=username, password=password)
    api.login()

Error/Debug Log:

File "/Library/Python/2.7/site-packages/instagram_web_api/client.py", line 390, in login
login_res = self._make_request('https://www.instagram.com/accounts/login/ajax/', params=params)
File "/Library/Python/2.7/site-packages/instagram_web_api/client.py", line 291, in _make_request
raise ClientBadRequestError(msg, e.code)
ClientBadRequestError: HTTPError "Bad Request" while opening https://www.instagram.com/accounts/login/ajax/

xinhuang327 avatar May 16 '19 09:05 xinhuang327

@xinhuang327

Your issue report does not conform to the issue template that has been specified for this repo: https://raw.githubusercontent.com/ping/instagram_private_api/master/.github/ISSUE_TEMPLATE.md

Please edit your issue to comply with the template requirement.

This issue will be closed after 24 hours if no followup action is taken.


[This comment is auto-generated. ref=notemplate]

ping avatar May 16 '19 09:05 ping

I'm getting the same issue. Using version 1.6.0, with instagram_web_api. When I call: Client(auto_patch=True, drop_incompat_keys=False) I get ClientError: Unable to get rhx_gis from init request.

CharlesStrickland avatar May 16 '19 11:05 CharlesStrickland

Yes, the same issue. rhx_gis was expelled from IG response

mafanikio avatar May 16 '19 12:05 mafanikio

I've just run into the same issue and fixed it temporarily by using a static rhx_gis that I found online. Use something like this to get it working for now:

rhx_gis = self._extract_rhx_gis(init_res_content) or "4f8732eb9ba7d1c8e8897a75d6474d4eb3f5279137431b2aafb71fafe2abe178"

Edit: I've just tried using any string as a rhx_gis and everything still works neatly.

devkarim avatar May 16 '19 12:05 devkarim

I do not work with this library, but I also encountered this problem. With the help of js reverse, I seem to have found a solution.

In sharedData data is output on the page. See field config->viewerId. It is now transmitted instead of rhx_gis. The string to form md5 looks like: :{"id":"11612471724"}, where the numbers are viewerId

in this way, rhx_gis = md5(':{"id":"viewerId"}')

sharedData fragment:

{ "config": { "csrf_token": "xoKnKvuk9HXYAyO7nwNP9BQVxfxvlEvw", "viewerId": "11612471724" }, ...

Lightsider avatar May 16 '19 13:05 Lightsider

Based on two upper commenters:

line 317 in client.py

def _extract_rhx_gis(html):
    tmp_str = ':{"id":"'+f'{random.randint(10000000,99999999)}'+'"}'
    return hashlib.md5(b'tmp_str')

Will solve the problem

mafanikio avatar May 16 '19 14:05 mafanikio

Based on two upper commenters:

line 317 in client.py

def _extract_rhx_gis(html):
    tmp_str = ':{"id":"'+f'{random.randint(10000000,99999999)}'+'"}'
    return hashlib.md5(b'tmp_str')

Will solve the problem

I do not work with this library, but I also encountered this problem. With the help of js reverse, I seem to have found a solution.

In sharedData data is output on the page. See field config->viewerId. It is now transmitted instead of rhx_gis. The string to form md5 looks like: :{"id":"11612471724"}, where the numbers are viewerId

in this way, rhx_gis = md5(':{"id":"viewerId"}')

sharedData fragment:

{ "config": { "csrf_token": "xoKnKvuk9HXYAyO7nwNP9BQVxfxvlEvw", "viewerId": "11612471724" }, ...

You can actually put any string there and it will work, you don't have to extract anything. I've tried that myself though.

devkarim avatar May 16 '19 15:05 devkarim

Based on two upper commenters:

line 317 in client.py

def _extract_rhx_gis(html):
    tmp_str = ':{"id":"'+f'{random.randint(10000000,99999999)}'+'"}'
    return hashlib.md5(b'tmp_str')

Will solve the problem

This works for Python 3 Here is a solution which works on 2.7 for me

def _extract_rhx_gis(html):
        tmp_str = ':{"id":{"'+ "{}" +'"}}'.format(random.randint(10000000,99999999))
        return hashlib.md5(b'tmp_str')

iloveautomation avatar May 16 '19 15:05 iloveautomation

Had to make some changes to the code above to make it work:

def _extract_rhx_gis(html):
    tmp_str = ':{"id":"'+f'{random.randint(10000000,99999999)}'+'"}'
    return hashlib.md5(tmp_str.encode()).hexdigest()

b'tmp_str' is just the string 'tmp_str', which negates the point of generating one

Mithorium avatar May 18 '19 15:05 Mithorium

This is still an issue :)

test24853 avatar May 19 '19 03:05 test24853

I've put the fix that @Mithorium suggested in a fork and made a pull request into the base repository. Take it or leave it but I hope this can be resolved soon!

Instagram's changed their APIs out from under us before and it's always a bit of a fire drill getting them up and running again

thekensman avatar May 23 '19 21:05 thekensman

I'm still having this problem. I don't think the PR will be accepted though @thekensman , both lines of code added produce security issues in Codacy.

haipcl avatar May 27 '19 19:05 haipcl

Yes I'm not sure if there's a more secure fix to be had - the PR is there for convenience, or to inspire a more robust solution to replace it

thekensman avatar May 29 '19 17:05 thekensman

I've fixed this issue with creating new class and overwrite it's static method.

import hashlib
import string
import random

from instagram_web_api import Client

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())

pourya2374 avatar Jun 25 '19 06:06 pourya2374

for some reasons @pourya2374 's code gives me an error. So I edited it slightly to fix this error:

import hashlib
import string
import random

from instagram_web_api import Client

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()

javad94 avatar Jul 17 '19 02:07 javad94

This issue is still present, why isn't there any fix on the master branch yet?

aegroto avatar Feb 26 '20 21:02 aegroto

@javad94 it worked for me a few weeks but suddenly I'm getting the same error:

instagram_web_api.errors.ClientBadRequestError: HTTPError "Bad Request" while opening https://www.instagram.com/accounts/login/ajax/ 77db6e1c30c697195362aa4ed593

pyinto avatar May 15 '20 07:05 pyinto

Getting same error as @pyinto instagram_web_api.errors.ClientBadRequestError: HTTPError "Bad Request" while opening https://www.instagram.com/accounts/login/ajax

reformedot avatar May 19 '20 08:05 reformedot

@pyinto @reformedot Instagram does not allow sending a plain text password at accounts/login/ajax/ anymore, it requires enc_password parameter instead. Refer to this dilame/instagram-private-api#1010

caffeinatedgaze avatar May 27 '20 15:05 caffeinatedgaze

I provided a fix with code from @javad94

eracle avatar May 31 '20 08:05 eracle

@eracle I tried your fix, but as others described, now I'm getting:

instagram_web_api.errors.ClientBadRequestError: HTTPError "Bad Request" while opening https://www.instagram.com/accounts/login/ajax/

Not sure how to fix per the enc_password parameter, as @fenchelfen pointed out.

agucova avatar Jun 03 '20 01:06 agucova

No solutions ? i still have the same error as @agucova

Aassifh avatar Jun 10 '20 11:06 Aassifh

Here is the working solution as of 1 July, use MyClient instead of using the libraries class directly.

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

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):
        """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}"

        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

HashamGhuffary avatar Jul 01 '20 15:07 HashamGhuffary

No solutions ? i still have the same error as @agucova

Please take a look at the code above.

HashamGhuffary avatar Jul 01 '20 15:07 HashamGhuffary

No solutions ? i still have the same error as @agucova

Please take a look at the code above.

I'm sorry, but I'm being stupid. I don't know how to add cookies to them, I get an error.

Unexpected exception: _ _ init__ () accepts 1 to 2 positional arguments bat 3 were given

In the line:

api = MyClient(
                    username, password,
                    on_login=lambda x: self.onlogin_callback(x, self.file_name))

I'm just trying to create and make an example from here: https://github.com/ping/instagram_private_api/blob/master/examples/savesettings_logincallback.py

Ghostigme avatar Jul 10 '20 12:07 Ghostigme

No solutions ? i still have the same error as @agucova

Please take a look at the code above.

I'm sorry, but I'm being stupid. I don't know how to add cookies to them, I get an error.

Unexpected exception: _ _ init__ () accepts 1 to 2 positional arguments bat 3 were given

In the line:

api = MyClient(
                    username, password,
                    on_login=lambda x: self.onlogin_callback(x, self.file_name))

I'm just trying to create and make an example from here: https://github.com/ping/instagram_private_api/blob/master/examples/savesettings_logincallback.py

well actually you have to remove the self before 'onlogin_callback'

HashamGhuffary avatar Jul 10 '20 18:07 HashamGhuffary

Hey guys, this is web enc_password api:

https://leesoar.com/api-v1/ig?pub_key=20ed90203c457a2f9efc20820c2452403bff6424de39ff9cc928a751e07f6915&pub_id=229&pwd=xxx&t=1596703337&secret_key=a9ad0489a73146c68ec514ffce5cbaba

the secret_key have 200 times.

  • "t" is timestamp
  • "pwd" is password
  • "version" is pub_version, and default is 10
  • "action" default is "enc_password"

It returns: { "code": 1, "message": "ok", "enc_password": "#PWD_INSTAGRAM_BROWSER:10:1596703337:AQpQAH4W1vHVVa7730diM49fhY5Cn0CsgfGsZ5zA613WkRJDn3ujkqqwEdtnV7BrjgJsW3zinQ1OrSnTdgU2We72gWztHxM7OW2NSfvVR/4AGuCZGbR8mpk30wEzP4Z9tPLfxmr/o3Nmk6v7", "pub_key": "20ed90203c457a2f9efc20820c2452403bff6424de39ff9cc928a751e07f6915", "pub_id": "229", "pwd": "xxx", "version": 10, "t": "1596703337" }

Ninjala avatar Aug 06 '20 08:08 Ninjala

python 2.7 script version of @HashamGhuffary

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

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 = ''
        self.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

ycaty avatar Sep 22 '20 11:09 ycaty

I have the same issue. In on web mode and when I tried with Client() return rhx_gis error.

Any solution? Thanks!!

reloxo95 avatar Nov 03 '20 16:11 reloxo95

Here is the working solution as of 1 July, use MyClient instead of using the libraries class directly.

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

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):
        """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}"

        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

I'm a little confused? Where does this go? Does anything else need to be replaced so that the MyClient class will be used instead?

ajbenz18 avatar Jun 01 '21 01:06 ajbenz18

@ajbenz18 you should put that at top of your script.

javad94 avatar Jun 01 '21 12:06 javad94