Rayrun
← Back to Discord Forum

Basic authentication denied (401_unauthorized_access)

Hey everyone, I am using the following Python-Code, to login on a website with basic authentication, while using a proxy:

import asyncio
from playwright.async_api import async_playwright
from random import randrange

async def run(playwright):
    proxy = {"server": servername, "username" : username, "password" : password}
    
    chromium = playwright.chromium # or "firefox" or "webkit".
    browser = await chromium.launch(proxy=proxy)
    page = await browser.new_page(http_credentials={'username':'username', 'password':'password'})
    await page.goto("https://www.website-with-basic-authentication.com/")

    await page.wait_for_load_state("networkidle"); # This waits for the "networkidle"
    await page.screenshot(path="screenshot.png", full_page=False)
    await browser.close()

async def main():
    async with async_playwright() as playwright:
        await run(playwright)
asyncio.run(main())

If I use "chromium = playwright.chromium", I get the error 401_unauthorized_access when accessing the website, but if I use "chromium = playwright.firefox" the website can be accessed and the login is successful. I can't wrap my head around why this is the case and I need to use chrome as a web browser (this is just a test script for a larger scanner).

Does anyone have any advice on how to resolve this issue? Thanks in advance!

This thread is trying to answer question "Why does the user receive a 401_unauthorized_access error when using Chromium with Playwright for basic authentication, and how can this issue be resolved?"

2 replies

Another solution with using set_extra_http_headers

  1. Create a function to encode the username/password

`import base64

user_pass = "myuser:mypass"
encoded = base64.b64encode(user_pass.encode()).decode()
page.set_extra_http_headers(
    {"Authorization": "Basic " + encoded}
)`

2. Call the function before page.goto (this will add the auth header and login with basic auth)

Unless you are using a proxy, try to remove it. I think you want @bandito9274 's solution not a proxy config like the one you setup.

browser = await chromium.launch(proxy=proxy)
# to
browser = await chromium.launch()

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].