Skip to content

vkpymusic.TokenReceiver

A class that is responsible for performing authorization using the available login and password. It interacts with the VK API to obtain an access token. The TokenReceiver class provides methods for handling captcha, 2-factor authentication, and various error scenarios.

Attributes:

Name Type Description
client Client

The client object.

__login str

The login.

__password str

The password.

__token str

The token.

_logger Logger

The logger.

Example usage:

>>> receiver = TokenReceiver(login="my_username", password="my_password")
>>> if receiver.auth():
...    receiver.get_token()
...    receiver.save_to_config()

Functions

__init__(login, password, client='Kate', logger=create_logger(__name__))

Initialize TokenReceiver.

Parameters:

Name Type Description Default
login str

Login to VK.

required
password str

Password to VK.

required
client str

Client to VK (default value = "Kate").

'Kate'
logger Logger

Logger (default value = my logger).

create_logger(__name__)

request_auth(code=None, captcha=None)

Request auth from VK.

Parameters:

Name Type Description Default
code Optional[str]

Code from VK/SMS (default value = None).

None
captcha Optional[Tuple[str, str]]

Captcha (default value = None).

None

Returns:

Name Type Description
Response Response

Response from VK.

request_code(sid)

Request code from VK.

Parameters:

Name Type Description Default
sid Union[str, int]

Sid from VK.

required

Returns:

Name Type Description
Response Response

Response from VK.

auth(on_captcha=on_captcha_handler, on_2fa=on_2fa_handler, on_invalid_client=on_invalid_client_handler, on_critical_error=on_critical_error_handler)

Performs authorization using the available login and password. If necessary, interactively accepts a code from SMS or captcha.

Parameters:

Name Type Description Default
on_captcha Callable[[str], str]

Handler to captcha. Get url image. Return key.

on_captcha_handler
on_2fa Callable[[], str]

Handler to 2-factor auth. Return captcha.

on_2fa_handler
on_invalid_client Callable[[], None]

Handler to invalid client.

on_invalid_client_handler
on_critical_error Callable[[Any], None]

Handler to critical error. Get response.

on_critical_error_handler

Returns:

Name Type Description
bool bool

Boolean value indicating whether authorization was successful or not.

get_token()

Prints token in console (if authorisation was successful).

save_to_config(file_path='config_vk.ini')

Save token and user agent data in config (if authorisation was successful).

Parameters:

Name Type Description Default
file_path str

Filename of config (default value = "config_vk.ini").

'config_vk.ini'

create_path(file_path) staticmethod

Create path before and after this for different funcs.

Parameters:

Name Type Description Default
file_path str

Relative path to file.

required

Returns:

Name Type Description
str str

Absolute path to file.