Some checks are pending
Build and Deploy / build (push) Waiting to run
- Updated monitor.py to support both OAuth and Service Account - Created setup-oauth-local.py for easy local authorization - Created cronjob-oauth.yaml for OAuth-based deployment - Updated README with both authentication options - OAuth is now the recommended method (no key file needed)
134 lines
3.8 KiB
Python
134 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Google Search Console OAuth Setup Script
|
|
Generates OAuth credentials and stores refresh token
|
|
"""
|
|
|
|
import os
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def create_oauth_credentials():
|
|
"""Guide user through OAuth setup"""
|
|
|
|
print("=" * 70)
|
|
print("GOOGLE SEARCH CONSOLE - OAUTH SETUP (No Service Account Key Needed)")
|
|
print("=" * 70)
|
|
print()
|
|
print("This method uses OAuth 2.0 instead of service account keys.")
|
|
print("You'll authenticate once with your Google account.")
|
|
print()
|
|
|
|
# Step 1: Create credentials
|
|
print("STEP 1: Create OAuth Credentials")
|
|
print("-" * 70)
|
|
print("1. Go to: https://console.cloud.google.com")
|
|
print("2. Select/create project: manoonoils-monitoring")
|
|
print("3. Go to: APIs & Services → Credentials")
|
|
print("4. Click: Create Credentials → OAuth client ID")
|
|
print("5. Application type: Desktop app")
|
|
print("6. Name: GSC Monitor")
|
|
print("7. Click Create")
|
|
print("8. Download the JSON file (client_secret_*.json)")
|
|
print()
|
|
input("Press Enter when you have downloaded the credentials file...")
|
|
|
|
# Step 2: Get credentials file path
|
|
print()
|
|
print("STEP 2: Upload Credentials")
|
|
print("-" * 70)
|
|
print("Copy the downloaded file to this server:")
|
|
print()
|
|
print(" scp /path/to/client_secret_*.json doorwaysftw:/tmp/gsc-credentials.json")
|
|
print()
|
|
input("Press Enter after uploading...")
|
|
|
|
# Step 3: Run authorization
|
|
print()
|
|
print("STEP 3: Authorize Application")
|
|
print("-" * 70)
|
|
print("Running authorization flow...")
|
|
print()
|
|
|
|
# Create auth script
|
|
auth_script = """#!/usr/bin/env python3
|
|
import os
|
|
import json
|
|
import pickle
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
from google.auth.transport.requests import Request
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
|
|
CREDS_FILE = '/tmp/gsc-credentials.json'
|
|
TOKEN_FILE = '/tmp/gsc-token.pickle'
|
|
|
|
def main():
|
|
creds = None
|
|
|
|
if os.path.exists(TOKEN_FILE):
|
|
with open(TOKEN_FILE, 'rb') as token:
|
|
creds = pickle.load(token)
|
|
|
|
if not creds or not creds.valid:
|
|
if creds and creds.expired and creds.refresh_token:
|
|
creds.refresh(Request())
|
|
else:
|
|
flow = InstalledAppFlow.from_client_secrets_file(
|
|
CREDS_FILE, SCOPES)
|
|
creds = flow.run_local_server(port=0)
|
|
|
|
with open(TOKEN_FILE, 'wb') as token:
|
|
pickle.dump(creds, token)
|
|
|
|
print("\\n✅ Authorization successful!")
|
|
print(f"Token saved to: {TOKEN_FILE}")
|
|
|
|
# Save credentials info
|
|
creds_info = {
|
|
'token': creds.token,
|
|
'refresh_token': creds.refresh_token,
|
|
'token_uri': creds.token_uri,
|
|
'client_id': creds.client_id,
|
|
'client_secret': creds.client_secret,
|
|
'scopes': creds.scopes
|
|
}
|
|
|
|
with open('/tmp/gsc-token.json', 'w') as f:
|
|
json.dump(creds_info, f, indent=2)
|
|
|
|
print(f"Credentials saved to: /tmp/gsc-token.json")
|
|
print("\\nYou can now deploy the monitoring system!")
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
"""
|
|
|
|
# Save and run auth script
|
|
with open("/tmp/gsc-auth.py", "w") as f:
|
|
f.write(auth_script)
|
|
|
|
print("Authorization script created at: /tmp/gsc-auth.py")
|
|
print()
|
|
print("Run this on the server to authorize:")
|
|
print()
|
|
print(" ssh doorwaysftw")
|
|
print(" cd /tmp")
|
|
print(" python3 gsc-auth.py")
|
|
print()
|
|
print("This will open a browser for you to authorize the app.")
|
|
print("If running on a remote server without browser, use SSH tunnel:")
|
|
print()
|
|
print(" ssh -L 8080:localhost:8080 doorwaysftw")
|
|
print(" Then run python3 gsc-auth.py")
|
|
print()
|
|
|
|
|
|
def main():
|
|
create_oauth_credentials()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|