[Python] Google Calendar API on EC2 (AWS) - webbrowser.Error: could not locate runnable browser

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am developing a Django App where a user can access his Google Calendar using this script

    credentials = None
    token = os.path.join(folder, "token.pickle")

    if os.path.exists(token):
    with open(token, 'rb') as tk:
    credentials = pickle.load(tk)

    if not credentials or not credentials.valid:
    if credentials and credentials.expired and credentials.refresh_token:
    credentials.refresh(Request())
    else:
    credentials = os.path.join(folder, "credentials.json")
    flow = InstalledAppFlow.from_client_secrets_file( credentials, scopes=['openid', 'https://www.googleapis.com/auth/calendar'] )
    flow.authorization_url(
    # Recommended, enable offline access so that you can refresh an access token without
    # re-prompting the user for permission. Recommended for web server apps.
    access_type='offline',
    # Optional, enable incremental authorization. Recommended as a best practice.
    include_granted_scopes='true',
    # # Optional, if your application knows which user is trying to authenticate, it can use this
    # # parameter to provide a hint to the Google Authentication Server.
    login_hint=useremail,
    # Optional, set prompt to 'consent' will prompt the user for consent
    prompt='consent')

    flow.run_local_server()
    credentials = flow.credentials

    with open(token, 'wb') as tk:
    pickle.dump(credentials, tk)

    service = build('calendar', 'v3', credentials = credentials)


    When I test it on my local machine everything works fine. However, I run it on a EC2 instance on AWS I get this error:

    flow.run_local_server()
    File "/home/ubuntu/webapp/lib/python3.12/site-packages/google_auth_oauthlib/flow.py", line 447, in run_local_server
    webbrowser.get(browser).open(auth_url, new=1, autoraise=True)
    ^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/lib/python3.12/webbrowser.py", line 66, in get
    raise Error("could not locate runnable browser")
    webbrowser.Error: could not locate runnable browser


    It is breaking on the line flow.run_local_server(). Any idea of what I am doing wrong?

    Thank you!!

    Continue reading...

Compartilhe esta Página