diff --git a/vk-auth.py b/vk-auth.py new file mode 100644 index 0000000..de998e6 --- /dev/null +++ b/vk-auth.py @@ -0,0 +1,63 @@ +import vk_api +import getpass + + +def write_token(token: str): + out = "" + + is_found = False + try: + with open('.env', 'r') as f: + lines = f.readlines() + for line in lines: + if line.startswith('VK_ACCESS_TOKEN='): + out += f"VK_ACCESS_TOKEN=\"{token}\"\n" + is_found = True + else: + out += line + except Exception: + pass + + if not is_found: + out += f"\nVK_ACCESS_TOKEN=\"{token}\"\n" + + with open('.env', 'w') as f: + f.write(out) + + +def auth_handler(): + """ При двухфакторной аутентификации вызывается эта функция. + """ + + # Код двухфакторной аутентификации + key = input("Enter authentication code: ") + # Если: True - сохранить, False - не сохранять. + remember_device = True + + return key, remember_device + + +def main(): + login, password = input("Login >> "), getpass.getpass() + vk_session = vk_api.VkApi( + login, password, + # offline, friends, photos, status, messages + scope=(1 << 16) | (1 << 1) | (1 << 2) | (1 << 10) | (1 << 12), + app_id=2685278, # kate mobile + api_version='5.131', + auth_handler=auth_handler + ) + + try: + vk_session.auth() + print(f"Result: {vk_session.token}") + print("Rewrite access token in .env file...") + write_token(vk_session.token['access_token']) + print("Success!") + except vk_api.AuthError as error_msg: + print(error_msg) + + +if __name__ == '__main__': + main() +