Update pages/oauth_callback.py
Browse files- pages/oauth_callback.py +61 -26
pages/oauth_callback.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
-
# pages/oauth_callback.py -
|
| 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 credentials from environment
|
| 10 |
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
|
@@ -26,7 +26,19 @@ st.markdown("""
|
|
| 26 |
0% { transform: rotate(0deg); }
|
| 27 |
100% { transform: rotate(360deg); }
|
| 28 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
<div style="text-align: center; padding: 50px;">
|
| 31 |
<h1>π Authenticating...</h1>
|
| 32 |
<p>Please wait while we log you in with Google.</p>
|
|
@@ -36,20 +48,20 @@ st.markdown("""
|
|
| 36 |
|
| 37 |
# Process the OAuth callback
|
| 38 |
try:
|
| 39 |
-
# Get the authorization code from URL
|
| 40 |
# Try new method first, fallback to old
|
| 41 |
try:
|
| 42 |
query_params = st.query_params
|
|
|
|
| 43 |
except AttributeError:
|
| 44 |
query_params = st.experimental_get_query_params()
|
|
|
|
| 45 |
|
| 46 |
if "code" in query_params and GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET:
|
| 47 |
# Extract code (different format in old vs new)
|
| 48 |
-
if
|
| 49 |
-
# New method returns dict
|
| 50 |
code = query_params.get("code", "")
|
| 51 |
else:
|
| 52 |
-
# Old method returns dict with lists
|
| 53 |
code_list = query_params.get("code", [])
|
| 54 |
code = code_list[0] if code_list else ""
|
| 55 |
|
|
@@ -58,32 +70,42 @@ try:
|
|
| 58 |
st.stop()
|
| 59 |
|
| 60 |
# Exchange code for token
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
if "access_token" in tokens:
|
| 74 |
# Get user info
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
# Store in session
|
| 81 |
st.session_state.user_info = user_info
|
| 82 |
st.session_state.authenticated = True
|
| 83 |
st.session_state.login_time = datetime.now()
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Auto-redirect to main page
|
| 89 |
st.markdown("""
|
|
@@ -92,18 +114,31 @@ try:
|
|
| 92 |
window.location.href = "/";
|
| 93 |
}, 2000);
|
| 94 |
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
""", unsafe_allow_html=True)
|
| 96 |
|
| 97 |
else:
|
| 98 |
st.error("β Authentication failed. Please try again.")
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
elif not GOOGLE_CLIENT_ID or not GOOGLE_CLIENT_SECRET:
|
| 102 |
-
st.error("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
else:
|
| 105 |
st.error("β No authorization code received. Please try logging in again.")
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
st.error(f"β Error: {str(e)}")
|
| 109 |
-
st.write("Please try logging in again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pages/oauth_callback.py - COMPLETE FIXED 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="β³", layout="centered")
|
| 8 |
|
| 9 |
# Get credentials from environment
|
| 10 |
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
|
|
|
| 26 |
0% { transform: rotate(0deg); }
|
| 27 |
100% { transform: rotate(360deg); }
|
| 28 |
}
|
| 29 |
+
.success-box {
|
| 30 |
+
background: linear-gradient(135deg, #10b981, #34d399);
|
| 31 |
+
color: white;
|
| 32 |
+
padding: 2rem;
|
| 33 |
+
border-radius: 10px;
|
| 34 |
+
text-align: center;
|
| 35 |
+
margin: 2rem 0;
|
| 36 |
+
}
|
| 37 |
</style>
|
| 38 |
+
""", unsafe_allow_html=True)
|
| 39 |
+
|
| 40 |
+
# Initial loading message
|
| 41 |
+
st.markdown("""
|
| 42 |
<div style="text-align: center; padding: 50px;">
|
| 43 |
<h1>π Authenticating...</h1>
|
| 44 |
<p>Please wait while we log you in with Google.</p>
|
|
|
|
| 48 |
|
| 49 |
# Process the OAuth callback
|
| 50 |
try:
|
| 51 |
+
# Get the authorization code from URL
|
| 52 |
# Try new method first, fallback to old
|
| 53 |
try:
|
| 54 |
query_params = st.query_params
|
| 55 |
+
is_new_api = True
|
| 56 |
except AttributeError:
|
| 57 |
query_params = st.experimental_get_query_params()
|
| 58 |
+
is_new_api = False
|
| 59 |
|
| 60 |
if "code" in query_params and GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET:
|
| 61 |
# Extract code (different format in old vs new)
|
| 62 |
+
if is_new_api:
|
|
|
|
| 63 |
code = query_params.get("code", "")
|
| 64 |
else:
|
|
|
|
| 65 |
code_list = query_params.get("code", [])
|
| 66 |
code = code_list[0] if code_list else ""
|
| 67 |
|
|
|
|
| 70 |
st.stop()
|
| 71 |
|
| 72 |
# Exchange code for token
|
| 73 |
+
with st.spinner("Verifying credentials..."):
|
| 74 |
+
token_url = "https://oauth2.googleapis.com/token"
|
| 75 |
+
data = {
|
| 76 |
+
"client_id": GOOGLE_CLIENT_ID,
|
| 77 |
+
"client_secret": GOOGLE_CLIENT_SECRET,
|
| 78 |
+
"code": code,
|
| 79 |
+
"redirect_uri": "https://refat81-social-media-data-extractor-chatbot.hf.space/oauth_callback",
|
| 80 |
+
"grant_type": "authorization_code"
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
response = requests.post(token_url, data=data)
|
| 84 |
+
tokens = response.json()
|
| 85 |
|
| 86 |
if "access_token" in tokens:
|
| 87 |
# Get user info
|
| 88 |
+
with st.spinner("Loading your profile..."):
|
| 89 |
+
userinfo_url = "https://www.googleapis.com/oauth2/v3/userinfo"
|
| 90 |
+
headers = {"Authorization": f"Bearer {tokens['access_token']}"}
|
| 91 |
+
user_response = requests.get(userinfo_url, headers=headers)
|
| 92 |
+
user_info = user_response.json()
|
| 93 |
|
| 94 |
# Store in session
|
| 95 |
st.session_state.user_info = user_info
|
| 96 |
st.session_state.authenticated = True
|
| 97 |
st.session_state.login_time = datetime.now()
|
| 98 |
|
| 99 |
+
# Clear the loading message and show success
|
| 100 |
+
st.empty() # Clear loading message
|
| 101 |
+
|
| 102 |
+
st.markdown(f"""
|
| 103 |
+
<div class="success-box">
|
| 104 |
+
<h2 style="margin:0;">β
Login Successful!</h2>
|
| 105 |
+
<p style="font-size: 1.2rem; margin: 1rem 0;">Welcome, <strong>{user_info.get('name', 'User')}</strong>!</p>
|
| 106 |
+
<p>Redirecting to dashboard...</p>
|
| 107 |
+
</div>
|
| 108 |
+
""", unsafe_allow_html=True)
|
| 109 |
|
| 110 |
# Auto-redirect to main page
|
| 111 |
st.markdown("""
|
|
|
|
| 114 |
window.location.href = "/";
|
| 115 |
}, 2000);
|
| 116 |
</script>
|
| 117 |
+
|
| 118 |
+
<div style="text-align: center; margin-top: 2rem;">
|
| 119 |
+
<p>If you are not redirected automatically, <a href="/">click here</a>.</p>
|
| 120 |
+
</div>
|
| 121 |
""", unsafe_allow_html=True)
|
| 122 |
|
| 123 |
else:
|
| 124 |
st.error("β Authentication failed. Please try again.")
|
| 125 |
+
if "error" in tokens:
|
| 126 |
+
st.write(f"Error: {tokens['error']}")
|
| 127 |
|
| 128 |
elif not GOOGLE_CLIENT_ID or not GOOGLE_CLIENT_SECRET:
|
| 129 |
+
st.error("""
|
| 130 |
+
β Server configuration error.
|
| 131 |
+
|
| 132 |
+
Please contact administrator or check HuggingFace Space secrets.
|
| 133 |
+
""")
|
| 134 |
|
| 135 |
else:
|
| 136 |
st.error("β No authorization code received. Please try logging in again.")
|
| 137 |
|
| 138 |
except Exception as e:
|
| 139 |
st.error(f"β Error: {str(e)}")
|
| 140 |
+
st.write("Please try logging in again from the main page.")
|
| 141 |
+
|
| 142 |
+
# Footer
|
| 143 |
+
st.markdown("---")
|
| 144 |
+
st.caption("Social Media Data Extractor β’ Secure OAuth Authentication")
|