ping / instagram_private_api

A Python library to access Instagram's private API.
MIT License
2.96k stars 615 forks source link

How do I change a device id? #183

Closed eduardoegos closed 5 years ago

eduardoegos commented 5 years ago

Please follow the guide below


Before submitting an issue, make sure you have:

Which client are you using?


Describe your Question/Issue:

So i use the bot with a few of accounts of mine, i'm using an adaptation of a login that i found in the examples (code below), but i always get the same device with all of the accounts, i saw that there is a generate_deviceid() function with a seed argument within the Client class, but i'm not sure how to set it before connecting the Client, being that it is a class function. The code even gets the device_id when reusing auth settings, but again, not sure how to set a new id from the start.


Paste the output of python -V here:

Code:

def login_instagram(username, password):

    print('Versão do Cliente: {0!s}'.format(client_version))
    print("Login {}".format(username))

    device_id = None
    try:

        settings_file = 'credentials/'+ username +'.json'
        if not os.path.isfile(settings_file):
            # settings file does not exist
            print('Não há arquivo de credencial: {0!s}'.format(settings_file))

            # login new
            api = Client(
                username, password,
                on_login=lambda x: onlogin_callback(x, settings_file))
        else:
            with open(settings_file) as file_data:
                cached_settings = json.load(file_data, object_hook=from_json)
            print('Reutilizando credenciais: {0!s}'.format(settings_file))

            device_id = cached_settings.get('device_id')
            # reuse auth settings
            api = Client(
                username, password,
                settings=cached_settings)

    except (ClientCookieExpiredError, ClientLoginRequiredError) as e:
        print('ClientCookieExpiredError/ClientLoginRequiredError: {0!s}'.format(e))

        # Login expired
        # Do relogin but use default ua, keys and such
        api = Client(
            username, password,
            device_id=device_id,
            on_login=lambda x: onlogin_callback(x, settings_file))

    except ClientLoginError as e:
        print('ClientLoginError {0!s}'.format(e))
    except ClientError as e:
        print('ClientError {0!s} (Code: {1:d}, Response: {2!s})'.format(e.msg, e.code, e.error_response))
    except Exception as e:
        print('Unexpected Exception: {0!s}'.format(e))

    return api

Error/Debug Log:

dvingerh commented 5 years ago

Generate it before instantiating a new api object which accepts device_id as one of the possible arguments

There's an example right here: https://github.com/ping/instagram_private_api/blob/816c4945f80fcd64e69dd27b5e8e0df768c0f9c1/tests/test_private_api.py#L80

eduardoegos commented 5 years ago

amazing, thank you!!