Spaces:
Running
Running
File size: 5,305 Bytes
b5fb8d2 |
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 |
#!/usr/bin/env python3
"""
Fix OAuth setup for Google Drive RAG system
"""
import os
import json
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# Configuration
SCOPES = ['https://www.googleapis.com/auth/drive.file']
CREDENTIALS_FILE = 'credentials.json'
TOKEN_FILE = 'token.json'
def fix_oauth_setup():
"""Fix OAuth setup with proper redirect URIs"""
print("π§ Fixing OAuth Setup for Google Drive RAG")
print("=" * 50)
# Check if credentials file exists
if not os.path.exists(CREDENTIALS_FILE):
print(f"β {CREDENTIALS_FILE} not found!")
print("\nπ Please follow these steps:")
print("1. Go to: https://console.cloud.google.com/")
print("2. APIs & Services β Credentials")
print("3. Create Credentials β OAuth 2.0 Client IDs")
print("4. Application type: Desktop application")
print("5. Download as 'credentials.json'")
return False
# Delete old token file if it exists
if os.path.exists(TOKEN_FILE):
print(f"ποΈ Removing old token file: {TOKEN_FILE}")
os.remove(TOKEN_FILE)
print(f"β
Found {CREDENTIALS_FILE}")
try:
# Load and validate credentials
with open(CREDENTIALS_FILE, 'r') as f:
creds_data = json.load(f)
print("β
Credentials file is valid")
print(f" Client ID: {creds_data.get('client_id', 'N/A')[:20]}...")
# Check if it's a desktop application
if creds_data.get('installed'):
print("β
Desktop application credentials detected")
else:
print("β οΈ Warning: This doesn't look like desktop application credentials")
print(" Make sure you selected 'Desktop application' when creating credentials")
except json.JSONDecodeError:
print("β Invalid JSON in credentials file")
return False
except Exception as e:
print(f"β Error reading credentials: {e}")
return False
# Try authentication with different ports
ports_to_try = [8080, 8081, 8082, 0] # 0 means let the system choose
for port in ports_to_try:
try:
print(f"\nπ Trying authentication on port {port if port > 0 else 'auto'}...")
# Create flow
flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_FILE, SCOPES)
if port == 0:
# Let the system choose the port
creds = flow.run_local_server(port=0)
else:
# Try specific port
creds = flow.run_local_server(port=port)
print("β
Authentication successful!")
# Save credentials
with open(TOKEN_FILE, 'w') as token:
token.write(creds.to_json())
print(f"β
Credentials saved to {TOKEN_FILE}")
# Test the credentials
print("\nπ§ͺ Testing Google Drive access...")
from googleapiclient.discovery import build
service = build('drive', 'v3', credentials=creds)
results = service.files().list(pageSize=1, fields="files(id, name)").execute()
files = results.get('files', [])
print("β
Google Drive access successful!")
print(f" Found {len(files)} file(s) in your Drive")
return True
except Exception as e:
error_msg = str(e).lower()
if "redirect_uri_mismatch" in error_msg:
print(f"β Port {port} failed: redirect_uri_mismatch")
if port < 8082: # Don't show this message for the last attempt
print(" Trying next port...")
continue
else:
print(f"β Port {port} failed: {e}")
if port < 8082:
print(" Trying next port...")
continue
print("\nβ All authentication attempts failed!")
print("\nπ§ Manual Fix Required:")
print("1. Go to: https://console.cloud.google.com/")
print("2. APIs & Services β Credentials")
print("3. Edit your OAuth 2.0 Client ID")
print("4. Add these to 'Authorized redirect URIs':")
print(" - http://localhost:8080/")
print(" - http://localhost:8081/")
print(" - http://localhost:8082/")
print(" - http://127.0.0.1:8080/")
print(" - http://127.0.0.1:8081/")
print(" - http://127.0.0.1:8082/")
print("5. Save and try again")
return False
def main():
"""Main function"""
print("π OAuth Fix for Google Drive RAG System")
print("=" * 50)
if fix_oauth_setup():
print("\nπ OAuth setup fixed successfully!")
print("β
You can now run: python setup_google_drive_rag.py")
else:
print("\nβ OAuth setup failed")
print("π‘ Please follow the manual fix instructions above")
if __name__ == "__main__":
main()
|