Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebAPI: Unable to get rhx_gis from init request #170

Open
5 of 6 tasks
xinhuang327 opened this issue May 16, 2019 · 39 comments
Open
5 of 6 tasks

WebAPI: Unable to get rhx_gis from init request #170

xinhuang327 opened this issue May 16, 2019 · 39 comments
Labels
no template Did not follow issue/pr template.

Comments

@xinhuang327
Copy link

xinhuang327 commented May 16, 2019

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:

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

Which client are you using?

  • app (instagram_private_api/)
  • 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/
@ping ping added the no template Did not follow issue/pr template. label May 16, 2019
@ping
Copy link
Owner

ping commented May 16, 2019

@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]

@CharlesStrickland
Copy link

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.

@mafanikio
Copy link

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

@devkarim
Copy link

devkarim commented May 16, 2019

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.

@Lightsider
Copy link

Lightsider commented May 16, 2019

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" }, ...

@mafanikio
Copy link

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

@devkarim
Copy link

devkarim commented May 16, 2019

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.

@iloveautomation
Copy link

iloveautomation commented May 16, 2019

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

josephlbarnett added a commit to josephlbarnett/instagram_private_api that referenced this issue May 16, 2019
henry-lajoie-gps pushed a commit to henry-lajoie-gps/instagram_private_api that referenced this issue May 17, 2019
@Mithorium
Copy link

Mithorium commented May 18, 2019

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

@test24853
Copy link

This is still an issue :)

jordanmkoncz added a commit to jordanmkoncz/instagram_private_api that referenced this issue May 19, 2019
thekensman pushed a commit to thekensman/instagram_private_api that referenced this issue May 23, 2019
@thekensman
Copy link

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

@haipcl
Copy link

haipcl commented May 27, 2019

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.

@thekensman
Copy link

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

@pourya2374
Copy link

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

@javad94
Copy link

javad94 commented Jul 17, 2019

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

nickw444 added a commit to nickw444/instagram_private_api that referenced this issue Dec 8, 2019
@aegroto
Copy link

aegroto commented Feb 26, 2020

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

@pyinto
Copy link

pyinto commented May 15, 2020

@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

@reformedot
Copy link

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

@caffeinatedgaze
Copy link

caffeinatedgaze commented May 27, 2020

@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

@eracle
Copy link

eracle commented May 31, 2020

I provided a fix with code from @javad94

@agucova
Copy link

agucova commented Jun 3, 2020

@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.

@Aassifh
Copy link

Aassifh commented Jun 10, 2020

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

@HashamGhuffary
Copy link

HashamGhuffary commented Jul 1, 2020

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
Copy link

HashamGhuffary commented Jul 1, 2020

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

Please take a look at the code above.

@Ghostigme
Copy link

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

@HashamGhuffary
Copy link

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'

@Ninjala
Copy link

Ninjala commented Aug 6, 2020

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

@ycaty
Copy link

ycaty commented Sep 22, 2020

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

@reloxo95
Copy link

reloxo95 commented Nov 3, 2020

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

Any solution?
Thanks!!

@ajbenz18
Copy link

ajbenz18 commented Jun 1, 2021

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?

@javad94
Copy link

javad94 commented Jun 1, 2021

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

@DavideWiest
Copy link

DavideWiest commented Aug 28, 2022

Using this solution gave me a JSONDecodeError when using user_info2
Is this a function that you have be authenticated to use?

Traceback (most recent call last): File "c:\Users\DavWi\OneDrive\Desktop\projects\instadata\test2.py", line 38, in <module> print(fc.user_info2(1176013646)) File "C:\Users\DavWi\AppData\Local\Programs\Python\Python39\lib\site-packages\instagram_web_api\client.py", line 433, in user_info2 info = self._make_request(endpoint, query={'__a': '1'}) File "C:\Users\DavWi\AppData\Local\Programs\Python\Python39\lib\site-packages\instagram_web_api\client.py", line 284, in _make_request return json.loads(response_content) File "C:\Users\DavWi\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) File "C:\Users\DavWi\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\DavWi\AppData\Local\Programs\Python\Python39\lib\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)

Edit: It gives me the same error when logged in.

@ycaty
Copy link

ycaty commented Aug 30, 2022

@DavideWiest

you're better off using python 3.5 or 2.7

@ycaty
Copy link

ycaty commented Aug 30, 2022

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

For others running into issues, please note these 2 lines in the code i posted above ^_^ the syntax for formatting password changes from 2.7 to 3.5 ...

@DavideWiest
Copy link

Ok, thanks!

@DavideWiest
Copy link

@DavideWiest

you're better off using python 3.5 or 2.7

does this fix the issue or is it just better generally when using the package?

@Ladet02
Copy link

Ladet02 commented Sep 7, 2022

from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@DavideWiest
you're better off using python 3.5 or 2.7

does this fix the issue or is it just better generally when using the package?

Weere you able to get a solution?

@DavideWiest
Copy link

@Ladet02
I now use user_info and log in instead

@mohamedfullstackdevloper

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

it worked for me
the problem was the params var
params = {'username': self.username, 'password': self.password, 'queryParams': '{}'} # this no longer work
but as you provided
enc_password = f"#PWD_INSTAGRAM_BROWSER:0:{time}:{self.password}"
params = {'username': self.username, 'enc_password': enc_password, 'queryParams': '{}', 'optIntoOneTap': False}
this worked 100%
thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no template Did not follow issue/pr template.
Projects
None yet
Development

No branches or pull requests