Update pages/oauth_callback.py
Browse files- pages/oauth_callback.py +63 -25
pages/oauth_callback.py
CHANGED
|
@@ -1,31 +1,69 @@
|
|
| 1 |
-
# pages/oauth_callback.py
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
st.set_page_config(page_title="Authenticating...", page_icon="β³")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
<p>Please wait while we log you in with Google.</p>
|
| 10 |
-
<div class="spinner"></div>
|
| 11 |
-
</div>
|
| 12 |
|
| 13 |
-
|
| 14 |
-
.
|
| 15 |
-
border: 4px solid #f3f3f3;
|
| 16 |
-
border-top: 4px solid #4285F4;
|
| 17 |
-
border-radius: 50%;
|
| 18 |
-
width: 40px;
|
| 19 |
-
height: 40px;
|
| 20 |
-
animation: spin 2s linear infinite;
|
| 21 |
-
margin: 20px auto;
|
| 22 |
-
}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
""
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pages/oauth_callback.py - WORKING VERSION
|
| 2 |
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
st.set_page_config(page_title="Authenticating...", page_icon="β³")
|
| 8 |
|
| 9 |
+
# Get Google credentials from environment
|
| 10 |
+
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
| 11 |
+
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
st.title("π Authenticating...")
|
| 14 |
+
st.write("Please wait while we log you in with Google.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Get the authorization code from URL
|
| 17 |
+
try:
|
| 18 |
+
query_params = st.experimental_get_query_params()
|
| 19 |
+
|
| 20 |
+
if "code" in query_params:
|
| 21 |
+
code = query_params["code"][0]
|
| 22 |
+
|
| 23 |
+
with st.spinner("Exchanging code for token..."):
|
| 24 |
+
# Exchange code for access token
|
| 25 |
+
token_url = "https://oauth2.googleapis.com/token"
|
| 26 |
+
data = {
|
| 27 |
+
"client_id": GOOGLE_CLIENT_ID,
|
| 28 |
+
"client_secret": GOOGLE_CLIENT_SECRET,
|
| 29 |
+
"code": code,
|
| 30 |
+
"redirect_uri": "https://refat81-social-media-data-extractor-chatbot.hf.space/oauth_callback",
|
| 31 |
+
"grant_type": "authorization_code"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
response = requests.post(token_url, data=data)
|
| 35 |
+
tokens = response.json()
|
| 36 |
+
|
| 37 |
+
if "access_token" in tokens:
|
| 38 |
+
# Get user info from Google
|
| 39 |
+
userinfo_url = "https://www.googleapis.com/oauth2/v3/userinfo"
|
| 40 |
+
headers = {"Authorization": f"Bearer {tokens['access_token']}"}
|
| 41 |
+
user_response = requests.get(userinfo_url, headers=headers)
|
| 42 |
+
user_info = user_response.json()
|
| 43 |
+
|
| 44 |
+
# Store user info in session
|
| 45 |
+
st.session_state.user_info = user_info
|
| 46 |
+
st.session_state.authenticated = True
|
| 47 |
+
st.session_state.login_time = datetime.now()
|
| 48 |
+
|
| 49 |
+
st.success("β
Login successful!")
|
| 50 |
+
st.write(f"Welcome, {user_info.get('name', 'User')}!")
|
| 51 |
+
|
| 52 |
+
# Redirect to main app
|
| 53 |
+
st.markdown("""
|
| 54 |
+
<script>
|
| 55 |
+
setTimeout(function() {
|
| 56 |
+
window.location.href = "/";
|
| 57 |
+
}, 2000);
|
| 58 |
+
</script>
|
| 59 |
+
""", unsafe_allow_html=True)
|
| 60 |
+
|
| 61 |
+
else:
|
| 62 |
+
st.error("β Authentication failed")
|
| 63 |
+
st.write("Error details:", tokens)
|
| 64 |
+
else:
|
| 65 |
+
st.error("No authorization code received")
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"Error during authentication: {str(e)}")
|
| 69 |
+
st.write("Please try logging in again.")
|