Google Authentication with google-auth-oauthlib

Good morning community, I present my strange case using the library google-auth-oauthlib · PyPI supported by the tutorial of the great Fanilo Andrianasolo https://www.youtube.com/watch?v=0M4K53XBsjo

I will tell you my case in detail in case you can help me solve the problem.

Create a test application to apply Google authentication, follow all the steps indicated in the Fanilo tutorial.

  • I have the project on Google Cloud
  • I created the OAuth Consent Screen
  • I included the permissions and the test email accounts
  • I created the OAuth 2.0 Client ID credential and downloaded the JSON
  • I created my streamlit project with the following libraries:
Python 3.12

# Libraries for Streamlit
streamlit==1.37.1

# Libraries for Google
google-auth-oauthlib==1.2.1
  • I created the secrets.toml file
  • I put my GCP credentials JSON file in the project
  • I created my script as follows:
import requests
import streamlit as st
from google.oauth2 import id_token
from google_auth_oauthlib import get_user_credentials

st.title(':closed_lock_with_key: Prueba Autenticación con Google')


def login_callback():
    credentials = get_user_credentials(
        client_id=st.secrets.client_id,
        client_secret=st.secrets.client_secret,
        scopes=[
            'openid',
            'https://www.googleapis.com/auth/userinfo.email',
            'https://www.googleapis.com/auth/userinfo.profile',
            "https://www.googleapis.com/auth/calendar.events.readonly",
        ],
        minimum_port=8501,
        maximum_port=8502,
    )
    print(f"credentials; {credentials}")
    st.session_state.credentials = credentials


st.button(':key: Login',
          type='primary',
          on_click=login_callback)

print(f'st.session_state: {st.session_state}')

if 'credentials' in st.session_state:
    id_info = id_token.verify_token(
        st.session_state.credentials.id_token,
        requests.Request(),
    )
    st.json(id_info)
  • I run it locally
streamlit run .\prueba_google_auth_01.py

image

  • Normally load the application

I complete the authentication process without problems, but when I redirect to the application page, after logging in, the logged in user information is not streamed, as if the application was reloaded and everything was lost.

If you notice, I put a print after the method call

It doesn’t print anything, I show you the console output after login

And this is how the page looks after authentication:

Why does this happen? How can I fix it?

I thought it was my code and I downloaded the source code from the Fanilo tutorial (GitHub - andfanilo/streamlit-google-authentication-tests: Exploring different ways for Google Authentication in Streamlit), I put my credentials in and the same thing happens, why did it work in the tutorial and not in mine?

Thank you so much for all the help you can give me.

Hey!
Didn’t have time to test thoroughly but here are my hunches:

  • minimum_port and maximum_port should not override your Streamlit port. They’re for the Flask server that is going to be generated by the get_user_credentials code to catch the redirect, so you don’t want that Flask server to override Streamlit. That’s why I put 9000-9001 so flask appears on http://localhost:9000, then I put http://localhost:9000 as a redirect URI in the Credentials authorised redirect URIs
  • since you are using Streamlit secrets (.streamlit/secrets.toml file) to read the client_id and client_secret into the code, you don’t actually need the JSON secret, this is for the second use case
  • Last I think you will be missing the Google Drive read scope later on, you’re using Google Calendar read in the code. There’s the list here, and I see there are a lot of code snippets for after you are authenticated

Hope it makes sense. You can also read this README for my configs.

Good luck!

Thank you very much Fanilo, your observations are very accurate, it was a silly thing, I just changed the minimum port to 9000 and the maximum to 9001, and included it in the GCP credentials configuration and it worked.

Thank you very much for your quick response. Success

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.