blazingbunny commited on
Commit
ec26d3d
·
verified ·
1 Parent(s): e7f5ef4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -1,28 +1,48 @@
1
  import streamlit as st
2
  from requests_oauthlib import OAuth2Session
 
3
 
4
- # ... (Load client ID and client secret securely)
 
 
 
 
5
 
6
  # OAuth Configuration
7
- authorization_base_url = 'https://accounts.google.com/o/oauth2/v2/auth'
8
- token_url = 'https://www.googleapis.com/oauth2/v4/token'
9
- redirect_uri = 'your_huggingface_spaces_app_url'
10
- scopes = ['https://www.googleapis.com/auth/webmasters.readonly']
11
 
12
- # ... (Streamlit UI - similar to before)
 
 
 
 
13
 
14
  if st.button("Fetch Data"):
15
- google_oauth = OAuth2Session(client_id, scopes=scopes, redirect_uri=redirect_uri)
16
-
17
- # Step 1: User authorization
18
- authorization_url, state = google_oauth.authorization_url(authorization_base_url, access_type="offline", prompt="select_account")
19
  st.write("Please authenticate with Google:", authorization_url)
20
 
21
- # Step 2: Handle redirect and fetch tokens (modify after user clicks the button)
22
  redirect_response = st.text_input("Paste the full redirect URL here:")
23
- token = google_oauth.fetch_token(token_url, client_secret=client_secret, authorization_response=redirect_response)
24
 
25
- # Step 3: Use token in API requests
26
  credentials = google_oauth.authorized
27
  service = build('webmasters', 'v3', credentials=credentials)
28
- # ... (rest of your API request logic)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from requests_oauthlib import OAuth2Session
3
+ import os # For loading credentials from environment variables
4
 
5
+ # ************ SECURITY: Replace placeholders with your actual credentials ***************
6
+ CLIENT_ID = os.environ.get('GOOGLE_CLIENT_ID') # Obtain from Google Cloud Console
7
+ CLIENT_SECRET = os.environ.get('GOOGLE_CLIENT_SECRET') # Obtain from Google Cloud Console
8
+ REDIRECT_URI = 'https://your-huggingface-space-app-url.hf.space' # Must match Google Project setting
9
+ # ************************************************************************************
10
 
11
  # OAuth Configuration
12
+ AUTHORIZATION_BASE_URL = 'https://accounts.google.com/o/oauth2/v2/auth'
13
+ TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token'
14
+ SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
 
15
 
16
+ # Streamlit UI
17
+ st.title("Search Analytics Viewer")
18
+ site_url = st.text_input("Enter your website URL:", "https://www.example.com")
19
+ start_date = st.date_input("Start Date")
20
+ end_date = st.date_input("End Date")
21
 
22
  if st.button("Fetch Data"):
23
+ # Initiate OAuth Flow
24
+ google_oauth = OAuth2Session(CLIENT_ID, scopes=SCOPES, redirect_uri=REDIRECT_URI)
25
+ authorization_url, state = google_oauth.authorization_url(AUTHORIZATION_BASE_URL, access_type="offline", prompt="select_account")
 
26
  st.write("Please authenticate with Google:", authorization_url)
27
 
28
+ # Handle Redirect
29
  redirect_response = st.text_input("Paste the full redirect URL here:")
30
+ token = google_oauth.fetch_token(TOKEN_URL, client_secret=CLIENT_SECRET, authorization_response=redirect_response)
31
 
32
+ # Use Authorized Session to Make API Requests
33
  credentials = google_oauth.authorized
34
  service = build('webmasters', 'v3', credentials=credentials)
35
+
36
+ request = {
37
+ 'startDate': start_date.strftime("%Y-%m-%d"),
38
+ 'endDate': end_date.strftime("%Y-%m-%d"),
39
+ 'dimensions': ['query', 'page'],
40
+ 'siteUrl': site_url
41
+ }
42
+ response = service.searchanalytics().query(siteUrl=request['siteUrl'], body=request).execute()
43
+
44
+ # Display Search Analytics Data (Customize this!)
45
+ if 'rows' in response:
46
+ st.dataframe(response['rows'])
47
+ else:
48
+ st.write('No data found')