Search code examples
pythonpython-3.xinstagraminstagram-api

How to correctly generate x-instagram-gis


I have written the following function in Python3.7 to generate x-instagram-gis. According to my research regarding this topic I have gathered that I only need the rhx_gis and variables (id: profile_id, first: int<50, after: end_cursor) to generate the x-instagram-gis.

def generate_x_instagram_gis(rhx_gis, cursor, profile_id):
    params = {
        "id": profile_id,
        "first": 12,
        "after": cursor,
    }
    json_params = json.dumps(params, separators=(',', ':'))
    values = "{}:{}".format(rhx_gis, json_params)
    return hashlib.md5(values.encode('utf-8')).hexdigest()

Running the following should return: 90bd6b662f328642477076d92d599064

rhx_gis = "7733066781d53e86a089eeb454c5446d"
cursor = "QVFBZWRqS0RnbGMtaXJhQzhlRW01R0I2YngtVXNQOGRTZzdHZEdseGcyVE1MdUxFYmYyY011Zkx6dFZtQUlsYWNvRl9DWnhtalpXZ2daSU5YQnFNTFBGRg=="
profile_id = "6822549659" #https://www.instagram.com/kimimatiasraikkonen/

print(generate_x_instagram_gis(rhx_gis, cursor, profile_id))

But it returns: f5e1e4be6612701d43523d707e36672b

For reference, these are the sources I've looked at:

I'm not entirely sure what I'm doing incorrectly, when I run this with my entire program it doesn't work and this is the only part which causes an issue after much testing. Another thing I noted is that the MD5 is different when running on Python3.7 and Python2.7


Solution

  • I have figured it out.

    The rhx_gis value is calculated based on the user-agent sent in the headers. The rhx_gis value I was obtaining was retrieved using python requests which sets its own user-agent (python-requests or something similar), whereas the rhx_gis value I was seeing on Postman was created using a different user-agent (set on Postman)

    To fix this issue I had to set the same user-agent in python requests as the one set on Postman.

    headers = {
        'User-Agent' : '' # user-agent here
    } 
    requests.get(url, headers=headers)