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()
>>> # or
>>> if asyncio.run(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__)
|
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 SYNC 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. |
auth_async(on_captcha=on_captcha_handler_async, on_2fa=on_2fa_handler_async, on_invalid_client=on_invalid_client_handler_async, on_critical_error=on_critical_error_handler_async)
async
Performs ASYNC 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]
|
ASYNC handler to captcha. Get url image. Return key. |
on_captcha_handler_async
|
on_2fa
|
Callable[[], str]
|
ASYNC handler to 2-factor auth. Return captcha. |
on_2fa_handler_async
|
on_invalid_client
|
Callable[[], None]
|
ASYNC handler to invalid client. |
on_invalid_client_handler_async
|
on_critical_error
|
Callable[[Any], None]
|
ASYNC handler to crit error. Get response. |
on_critical_error_handler_async
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Boolean value indicating whether authorization was successful or not. |
get_token()
Returns the token if exists.
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. |