From 4a04f7eda41b86e81cf6cbb0061699be1c660b26 Mon Sep 17 00:00:00 2001 From: VladislavOstapov Date: Fri, 24 Mar 2023 23:42:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=B0=D1=83=D1=82=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D0=B8,=20=D1=81?= =?UTF-8?q?=D0=BA=D1=80=D0=B8=D0=BF=D1=82=20=D1=81=D0=B0=D0=BC=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=BF=D0=B8=D1=88=D0=B5=D1=82=20?= =?UTF-8?q?(=D0=B8=D0=BB=D0=B8=20=D0=B2=D1=81=D1=82=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82)=20=D1=82=D0=BE=D0=BA=D0=B5=D0=BD=20=D0=B2=20.env=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vk-auth.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 vk-auth.py 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() +