{"id":26397651,"url":"https://github.com/andrepradika/bot-login-otp-verification","last_synced_at":"2025-03-17T12:18:32.141Z","repository":{"id":282142182,"uuid":"947623195","full_name":"andrepradika/bot-login-otp-verification","owner":"andrepradika","description":"This project automates the login process for a website that requires OTP-based authentication. It uses Playwright 🎭 for browser automation and Gmail API 📩 to fetch OTP codes from an email account.","archived":false,"fork":false,"pushed_at":"2025-03-13T01:48:50.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T02:34:11.094Z","etag":null,"topics":["automation","login","otp"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrepradika.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-13T01:35:25.000Z","updated_at":"2025-03-13T01:48:53.000Z","dependencies_parsed_at":"2025-03-13T02:44:16.140Z","dependency_job_id":null,"html_url":"https://github.com/andrepradika/bot-login-otp-verification","commit_stats":null,"previous_names":["andrepradika/bot-login-otp-verification"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbot-login-otp-verification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbot-login-otp-verification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbot-login-otp-verification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrepradika%2Fbot-login-otp-verification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrepradika","download_url":"https://codeload.github.com/andrepradika/bot-login-otp-verification/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244031119,"owners_count":20386534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","login","otp"],"created_at":"2025-03-17T12:18:31.679Z","updated_at":"2025-03-17T12:18:32.127Z","avatar_url":"https://github.com/andrepradika.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🔐 Automated Login \u0026 OTP Verification  \n\nThis project automates the **login process** for a website that requires **OTP-based authentication**. It uses **Playwright** 🎭 for browser automation and **Gmail API** 📩 to fetch OTP codes from an email account.\n\n---\n\n## 🚀 Features  \n\n✅ **Automated Login** – Enters username and password like a real user.  \n✅ **OTP Handling** – Fetches the verification code from Gmail.  \n✅ **Randomized Delays** – Mimics human behavior to bypass detection.  \n✅ **Retry Mechanism** – Handles failed login attempts.  \n✅ **Playwright Automation** – Uses a headless browser for efficiency.  \n\n---\n\n## 🏷️ Topics  \n\n🔹 **Web Automation** 🌍 – Automate login flows using Playwright.  \n🔹 **Email API** 📩 – Fetch OTP codes from Gmail.  \n🔹 **Python Scripting** 🐍 – Automate repetitive tasks.  \n🔹 **Bot Detection Bypass** 🔄 – Mimic real user behavior with random delays.  \n🔹 **Cybersecurity \u0026 Testing** 🔑 – Secure login testing and automation.  \n\n---\n\n## 📂 Setup  \n\n### 1️⃣ Install Dependencies  \n```bash\npip install playwright google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client\nplaywright install\n```\n\n### 2️⃣ Set Up Gmail API  \n- Enable Gmail API in **Google Cloud Console**.  \n- Download `credentials.json` and place it in the `utils/` folder.  \n\n### 3️⃣ Run the Script  \n```bash\npython login.py\n```\n\n---\n\n## 📝 Code Explanation  \n\n### **1️⃣ Import Dependencies**  \n```python\nfrom __future__ import print_function\nfrom utils.getGmailServcies import get_gmail_service\nfrom utils.getVerificationCode import get_verification_code\nfrom utils.utils import click_with_retry\nimport time\nimport random\nfrom playwright.sync_api import sync_playwright\n```\n- `playwright.sync_api` – Automates browser actions.  \n- `get_gmail_service` – Connects to Gmail API.  \n- `get_verification_code` – Extracts OTP from emails.  \n- `time.sleep()` \u0026 `random.uniform()` – Adds **human-like delays**.  \n\n---\n\n### **2️⃣ Define Random Delays**  \n```python\ndef random_delay(min_t=5, max_t=10):\n    time.sleep(random.uniform(min_t, max_t))\n```\n- Adds **random wait times** to **avoid bot detection**.  \n\n---\n\n### **3️⃣ Automate Login Process**  \n```python\ndef login(targetID, targetPass, max_login_retries=3):\n    with sync_playwright() as p:\n        browser = p.chromium.launch(headless=False)\n        page = browser.new_page(user_agent=\"Mozilla/5.0 ...\")\n        \n        print(\"Opening login page...\")\n        page.goto(\"https://target/auth/login/\")\n        random_delay()\n```\n- Opens the **login page** using a **realistic browser**.  \n- Uses a **custom user-agent** to **avoid bot detection**.  \n\n---\n\n### **4️⃣ Enter Username \u0026 Password**  \n```python\n        print(\"Entering username...\")\n        page.locator(\"input[name='username']\").click()\n        for char in targetID:\n            page.type(\"input[name='username']\", char, delay=random.randint(80, 150))  \n        random_delay()\n```\n- Simulates **typing speed** to **mimic human behavior**.  \n\n---\n\n### **5️⃣ Login Retry Mechanism**  \n```python\n        login_attempts = 0\n        while login_attempts \u003c max_login_retries:\n            print(f\"Attempting login ({login_attempts + 1})...\")\n            page.locator(\"button[type='submit']\").hover()\n            random_delay()\n            page.keyboard.press(\"Enter\")\n            random_delay()\n            \n            if \"extra-verification\" in page.url:\n                print(\"Login successful, proceeding to OTP verification.\")\n                break\n            \n            error_element = page.locator(\"text='Terjadi kesalahan, silahkan coba lagi.'\")\n            if error_element.count() \u003e 0:\n                print(f\"Login failed. Retrying ({login_attempts + 1}/{max_login_retries})...\")\n                login_attempts += 1\n                random_delay()\n            else:\n                break  \n```\n- **Retries login** up to 3 times **if it fails**.  \n- **Checks the page URL** to see if **OTP verification is required**.  \n\n---\n\n### **6️⃣ Fetch OTP from Gmail**  \n```python\n        print(\"Fetching OTP...\")\n        service = get_gmail_service()\n        verification_code = get_verification_code(service)\n        print(f'Your OTP: {verification_code}')\n```\n- Uses **Gmail API** to retrieve the **OTP code**.  \n\n---\n\n### **7️⃣ Enter OTP \u0026 Final Verification**  \n```python\n        otp_inputs = page.locator(\"input.otp-key\")\n        for i, digit in enumerate(verification_code):\n            otp_inputs.nth(i).click()\n            random_delay(0.2, 0.8)\n            otp_inputs.nth(i).type(digit, delay=random.randint(100, 200))\n            random_delay()\n\n        print(\"Submitting OTP...\")\n        page.locator(\"button[type='submit']\").hover()\n        random_delay()\n        page.keyboard.press(\"Enter\")\n        \n        print(\"Verifying final login status...\")\n        page.wait_for_load_state(\"networkidle\")\n        if \"dashboard\" in page.url:\n            print(\"Login successful. Accessing dashboard.\")\n        else:\n            print(\"Login might have failed. Check manually.\")\n```\n- **Types the OTP digits one by one**.  \n- **Submits the OTP** and checks if the **dashboard loads**.  \n\n---\n\n## License\n\nMIT License (or your preferred license)\n\n## Author\nandrepradika\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrepradika%2Fbot-login-otp-verification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrepradika%2Fbot-login-otp-verification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrepradika%2Fbot-login-otp-verification/lists"}