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