Simple Streamlit Google OAuth

Using this wonderful code Implementing Google OAuth in Streamlit | by Duc Anh Bui | Towards Data Science, I refactor it to simply use as pip.

Just do:
pip install git+https://github.com/hunkim/streamlit-google-oauth

Then add login code:

login_info = oauth.login(
        client_id=client_id,
        client_secret=client_secret,
        redirect_uri=redirect_uri,
        login_button_text="Continue with Google",
        logout_button_text="Logout",
    )

if login_info:
        user_id, user_email = login_info
        st.write(f"Welcome {user_email}")
else:
        st.write("Please login")

That’s it.

Any comments, and contributions are welcome at GitHub - hunkim/streamlit-google-oauth: An example Streamlit application that incorporates Google OAuth 2.0.

Actually, these buttons are ugly. Please feel free to send us PR.

14 Likes

Looks great
For some reason it lets every user in, not only the ones under “Test Users” (and the app is set to testing mode)

How can I save the user session state here?
Because as I reload the page I have to authenticate again.
Thanks!

This is how you maintain session state.


The Code is working fine in localhost. I really appreciate the work. But when I hosted it on streamlit cloud, I am getting the above. I have added the keys in secrets as well. Seems like it’s the issue with GOOGLE_REDIRECT_URI key. Any suugestions please. GitHub - Harisathwik/streamlitoauth is the link to code. Just few lines of code. https://examonoaut.streamlit.app/ is the link. The App reads the email id and prints Welcome EMAIL_ID!

Did you change the GOOGLE_REDIRECT_URI in both your code as well as your google cloud API console?

Yes

I have the same issue.
It seems that REDIRECT open a new streamlit session and hence clears the previous session’s variables.
Did u arrive at workaround?
Yahia

I used cookies to maintain user session even if he reloads the website or visit it after a day

Use this library to integrate google auth in your application

import streamlit as st
from google_auth import Google_auth

client_id = “hasjh5jk498ufiu3h89g8-aghdszjhk3k.apps.googleusercontent.com
client_secret = “afsghfbkhfdjdsgfdjhfjkfhjkfhkjhkjdhks”

login = Google_auth(clientId=client_id, clientSecret=client_secret)

if login == “authenticated”:
# your streamlit applciation
pass

else:
st.warning(“login failed”)

1 Like

use this imports

from google_auth.google_auth import GoogleAuth

found any workaround/fix for this?