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
- 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.