Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,48 @@
|
|
1 |
import streamlit as st
|
2 |
from requests_oauthlib import OAuth2Session
|
|
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# OAuth Configuration
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
scopes = ['https://www.googleapis.com/auth/webmasters.readonly']
|
11 |
|
12 |
-
#
|
|
|
|
|
|
|
|
|
13 |
|
14 |
if st.button("Fetch Data"):
|
15 |
-
|
16 |
-
|
17 |
-
|
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 |
-
#
|
22 |
redirect_response = st.text_input("Paste the full redirect URL here:")
|
23 |
-
token = google_oauth.fetch_token(
|
24 |
|
25 |
-
#
|
26 |
credentials = google_oauth.authorized
|
27 |
service = build('webmasters', 'v3', credentials=credentials)
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|