|
1 | 1 | import streamlit as st |
2 | | -import tempfile |
3 | | -import os |
4 | | -from google.oauth2 import service_account |
| 2 | +import pandas as pd |
5 | 3 | from googleapiclient.discovery import build |
6 | 4 | from googleapiclient.http import MediaFileUpload |
| 5 | +from google.oauth2.credentials import Credentials |
| 6 | +from google.auth.transport.requests import Request |
| 7 | +import os |
7 | 8 |
|
8 | | -# Set page title |
9 | | -st.set_page_config(page_title="Scientific Slide Library", page_icon="🔬") |
10 | | -st.title("🔬 Scientific Slide Library Uploader") |
11 | | -st.markdown("Use the form below to upload your slides and update the tracking sheet.") |
| 9 | +# --- 1. SETTINGS & AUTHENTICATION --- |
12 | 10 |
|
13 | | -# 1. AUTHENTICATION (Using Streamlit Secrets) |
14 | | -try: |
15 | | - # Get IDs and JSON contents from Secrets |
16 | | - SHEET_ID = st.secrets["SHEET_ID"] |
17 | | - FOLDER_ID = st.secrets["FOLDER_ID"] |
18 | | - |
19 | | - # Map the service account keys from the [gcp_service_account] section in Secrets |
20 | | - credentials_info = st.secrets["gcp_service_account"] |
| 11 | +# Replace these with your actual IDs |
| 12 | +SPREADSHEET_ID = st.secrets["SHEET_ID"] |
| 13 | +FOLDER_ID = st.secrets["FOLDER_ID"] |
| 14 | + |
| 15 | +def get_gdrive_service(): |
| 16 | + """Authenticates using the OAuth Refresh Token from st.secrets""" |
| 17 | + creds_info = st.secrets["google_oauth"] |
21 | 18 |
|
22 | | - creds = service_account.Credentials.from_service_account_info( |
23 | | - credentials_info, |
24 | | - scopes=[ |
25 | | - "https://www.googleapis.com/auth/spreadsheets", |
26 | | - "https://www.googleapis.com/auth/drive.file" |
27 | | - ] |
| 19 | + # Create credentials object using your specific Refresh Token |
| 20 | + creds = Credentials( |
| 21 | + token=None, # Access token starts empty |
| 22 | + refresh_token=creds_info["refresh_token"], |
| 23 | + client_id=creds_info["client_id"], |
| 24 | + client_secret=creds_info["client_secret"], |
| 25 | + token_uri="https://oauth2.googleapis.com/token" |
28 | 26 | ) |
| 27 | + |
| 28 | + # Refresh the access token automatically if it's expired |
| 29 | + if not creds.valid: |
| 30 | + creds.refresh(Request()) |
| 31 | + |
| 32 | + # Build the services |
| 33 | + drive_service = build('drive', 'v3', credentials=creds) |
| 34 | + sheets_service = build('sheets', 'v4', credentials=creds) |
29 | 35 |
|
30 | | - # Build the API services |
31 | | - sheets_service = build("sheets", "v4", credentials=creds) |
32 | | - drive_service = build("drive", "v3", credentials=creds) |
| 36 | + return drive_service, sheets_service |
| 37 | + |
| 38 | +# Initialize the services |
| 39 | +try: |
| 40 | + drive_service, sheets_service = get_gdrive_service() |
33 | 41 | except Exception as e: |
34 | | - st.error("⚠️ Authentication Error: Please check your Streamlit 'Secrets' configuration.") |
| 42 | + st.error(f"Authentication Failed: {e}") |
35 | 43 | st.stop() |
36 | 44 |
|
| 45 | +# --- APP INTERFACE START --- |
| 46 | +st.title("Scientific Slide Library") |
| 47 | +st.write("Upload your slides and details below.") |
| 48 | + |
37 | 49 | # 2. THE UPLOAD FORM |
38 | 50 | with st.form("upload_form", clear_on_submit=True): |
39 | | - name = st.text_input("Presenter Name") |
| 51 | + person = st.text_input("Your name") |
| 52 | + name = st.text_input("Name of Presentation") |
40 | 53 | description = st.text_area("Topic/Description") |
41 | 54 | keywords = st.text_input("Keywords (comma separated)") |
42 | 55 |
|
|
91 | 104 | # --- STEP B: UPDATE GOOGLE SHEET --- |
92 | 105 | try: |
93 | 106 | # Prepare row data |
94 | | - row_data = [[name, description, keywords, file_link]] |
| 107 | + row_data = [[name, description, keywords, file_link, person]] |
95 | 108 |
|
96 | 109 | sheets_service.spreadsheets().values().append( |
97 | 110 | spreadsheetId=SHEET_ID, |
|
0 commit comments